Database-driven testing in Python
Quality Labs just released PDbSeed, a Python framework for seeding a database. This is quite a hip tool during developer testing of applications which require a database. If you’ve ever heard of DbUnit, this is its Python cousin, man.
For example, here is a rough example of a PyUnit test case which refreshes the database on setup with a known dataset and then asserts the existence of a specific column value to verify the data was properly inserted.
import unittest
import pdbseed.core.dbseed as dbseed
class database_testcase(unittest.TestCase):
def setUp(self):
"""
setUp updates the database with the data found
in the dataset
"""
dbseed.RefreshOperation().execute(self._dataSet())
def testData(self):
"""
test case creates a new connection to db and
asserts a specific item from the xml file exists
"""
import MySQLdb
conn= MySQLdb.connect( host = "localhost", \
user = "words", passwd = "words", db = "words")
query = 'select word.part_of_speech from word \
where word.spelling = \'pugnacious\';'
curs = conn.cursor()
curs.execute(query)
wlist = curs.fetchall()
for word in wlist:
self.assertEqual('Adjective', word[0], 'Adj was not returned')
conn.close()
def _dataSet(self):
"""
method returns a dbseed dataset type.
"""
dbhost = 'localhost'
dbname = 'words'
dbuser = 'words'
dbpassword = 'words'
datasetFilename = 'C:/dev/prdbtesting/conf/words-seed.xml'
metaDataFilename = 'C:/dev/prdbtesting/conf/metadata.xml'
context = dbseed.ContextFactory.createContext('mysql', metaDataFilename, \
dbhost, dbname, dbuser, dbpassword)
return dbseed.FlatXMLFileRecordProducer(context, datasetFilename)
if __name__ == "__main__":
unittest.main()
This example reads two files. One is metadata file which describes a database schema and the other is a dataset which contains values to be inserted into the database.
For more information, see the PDbSeed documentation; additionally, the library bundles some example code. Dig it?
| Related odds and ends | ||
|---|---|---|
Wednesday 22 Feb 2006 | Developer Testing, Dynamic Languages, PyUnit, Python
Thanks for the heads up dude.