473,806 Members | 2,944 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python and database unittests

Hello,

I'm writing an application that interacts with a database. As I think
about how to write the unittests, I want them to be able to run
without actually having to access a live database. The pattern that
best describes this is here:

http://martinfowler.com/eaaCatalog/serviceStub.html

I have found http://qualitylabs.org/pdbseed/, which helps with
unittests for a live database. This isn't what I'm after.

Does anyone know about a module that acts as a database stub for
python unittests?

Thanks,
Daniel
Aug 26 '08 #1
9 6754
Daniel I don't know if it would work for your situation or not, but if
you are using Python 2.5, you could use the now built-in sqlite3
module. If you didn't even want to create a temporary database file
you could use the special memory-only syntax like this:
>>import sqlite3
conn =sqlite3.connec t(":memory:")
# use the connection
conn.close( )
--gordy
Aug 26 '08 #2
Hey gordy,

Thanks for the reply. I am actually using sqlite in part of my
application and I don't feel the need to stub that. Even though I
don't use the in memory option, it is still zero configuration so I
build up my database in each test then delete it.

However, the way I interact with the MySQL database is different than
the sqlite portion.

Thanks again,
Daniel
On Aug 26, 4:12*pm, gordyt <gor...@gmail.c omwrote:
Daniel I don't know if it would work for your situation or not, but if
you are using Python 2.5, you could use the now built-in sqlite3
module. *If you didn't even want to create a temporary database file
you could use the special memory-only syntax like this:
>import sqlite3
conn =sqlite3.connec t(":memory:")
# use the connection
conn.close()

--gordy
Aug 26 '08 #3
Daniel <daniel.watr... @gmail.comwrote :
Does anyone know about a module that acts as a database stub for
python unittests?
It's not database-specific, but the Mock module should help you here:

http://python-mock.sourceforge.net/

There's even an example on that page for mocking a database.
Aug 27 '08 #4
On Wed, Aug 27, 2008 at 4:55 AM, alex23 <wu*****@gmail. comwrote:
Daniel <daniel.watr... @gmail.comwrote :
>Does anyone know about a module that acts as a database stub for
python unittests?

It's not database-specific, but the Mock module should help you here:

http://python-mock.sourceforge.net/

There's even an example on that page for mocking a database.
--
http://mail.python.org/mailman/listinfo/python-list


--
Marco Bizzarri
http://iliveinpisa.blogspot.com/
Aug 27 '08 #5
On Wed, Aug 27, 2008 at 4:55 AM, alex23 <wu*****@gmail. comwrote:
Daniel <daniel.watr... @gmail.comwrote :
>Does anyone know about a module that acts as a database stub for
python unittests?

It's not database-specific, but the Mock module should help you here:

http://python-mock.sourceforge.net/

There's even an example on that page for mocking a database.
--
http://mail.python.org/mailman/listinfo/python-list
I strongly disagree on using mocks for a database; checking sequences
of SQL statement is fragile, painful, and leads you to frustration
when the actual SQL and the generated SQL do not match.

Regards
Marco

--
Marco Bizzarri
http://iliveinpisa.blogspot.com/
Aug 27 '08 #6
On Tue, Aug 26, 2008 at 11:35 PM, Daniel <da************ @gmail.comwrote :
Hello,

I'm writing an application that interacts with a database. As I think
about how to write the unittests, I want them to be able to run
without actually having to access a live database. The pattern that
best describes this is here:

http://martinfowler.com/eaaCatalog/serviceStub.html

I have found http://qualitylabs.org/pdbseed/, which helps with
unittests for a live database. This isn't what I'm after.

Does anyone know about a module that acts as a database stub for
python unittests?

Thanks,
Daniel
--
http://mail.python.org/mailman/listinfo/python-list

I think you're pointing to the wrong direction, if you want to make a
servicestub; the service should encapsulate your access to the
database (or whatever external resource you want to access), and,
after that, it should be transparent for you, more or less.

Regards
Marco
--
Marco Bizzarri
http://iliveinpisa.blogspot.com/
Aug 27 '08 #7
2008/8/27 alex23 <wu*****@gmail. com>:
Daniel <daniel.watr... @gmail.comwrote :
It's not database-specific, but the Mock module should help you here:

http://python-mock.sourceforge.net/

There's even an example on that page for mocking a database.
There's a number of mocking modules for Python - my current favorite
is Mox. See <http://tinyurl.com/57hzr4>.

--
Cheers,
Simon B.
si***@brunningo nline.net
http://www.brunningonline.net/simon/blog/
Aug 27 '08 #8
2008/8/27 Marco Bizzarri <ma************ @gmail.com>:
I strongly disagree on using mocks for a database; checking sequences
of SQL statement is fragile, painful, and leads you to frustration
when the actual SQL and the generated SQL do not match.
Clearly you need integration tests as well as unit tests, but the unit
tests ought to isolate the code under test, so stubbing out external
dependencies is the norm.

--
Cheers,
Simon B.
si***@brunningo nline.net
http://www.brunningonline.net/simon/blog/
GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues | Twitter: brunns
Aug 27 '08 #9
On Wed, Aug 27, 2008 at 10:26 AM, Simon Brunning
<si***@brunning online.netwrote :
2008/8/27 Marco Bizzarri <ma************ @gmail.com>:
>I strongly disagree on using mocks for a database; checking sequences
of SQL statement is fragile, painful, and leads you to frustration
when the actual SQL and the generated SQL do not match.

Clearly you need integration tests as well as unit tests, but the unit
tests ought to isolate the code under test, so stubbing out external
dependencies is the norm.

I agree with you about stubbing external dependencies; I'm just
suggesting to stub the stuff a little further, so that you're not
exposed to actual SQL code.

Regards
Marco

--
Cheers,
Simon B.
si***@brunningo nline.net
http://www.brunningonline.net/simon/blog/
GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues | Twitter: brunns
--
http://mail.python.org/mailman/listinfo/python-list


--
Marco Bizzarri
http://iliveinpisa.blogspot.com/
Aug 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

467
21730
by: mike420 | last post by:
THE GOOD: 1. pickle 2. simplicity and uniformity 3. big library (bigger would be even better) THE BAD:
105
5368
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does not work. It only works on lists and strings, for no obvious reason. Why not on all sequence types? Or, another example, the index() method has start and end parameters for lists and strings. The count() method also has start and end parameters...
3
2022
by: Richard Tew | last post by:
Hi, Stackless Python is now available for the recent release of Python 2.4.3 (final). You can either obtain the source code from the SVN repository or download the precompiled windows binaries. SVN: http://codespeak.net/svn/stackless/Python-2.4.3/dev Download: http://www.stackless.com/download
3
5278
by: Peter Larsen [CPH] | last post by:
Hi, I have a problem where the code in Global.asax.cs runs before the unittests (on test of course). In the global file, i load some data from a Oracle table and save it in HttpApplicationState. This is happening in the Application_start function. The following unittest show the beginning of the method, where the database mock is created. If this mock isn't created, the unittest will try to access
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10369
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10110
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3851
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.