Tutorials, Guides, Tips, and Tricks from Everyday Experiences

The following sections give quick, heavily commented, and ready-to-run examples that will give you a head start in creating a unit test framework in Python (pyUnit). First, we’ll take a look at a standalone module with several simple examples of unit tests. Then, we’ll take a look at a more complicated version that uses modules and test suites.

Here’s the first example. As long as you are running Python version 2.1 or greater, you can copy/paste this code, save it to unittest.py, and run it using python unittest.py.

'''This is a simple program to demonstrate how to create a unittest in
Python. For more information and documentation, please see the official
documentation page here: http://docs.python.org/library/unittest.html'''
import unittest #Include the pyUnit unittest framework
def add(a,b,c=0):
'''A simple adding function to demo unittest'''
return a+b+c
# The following is the class in which all functions will be ran by unittest
class AddTest(unittest.TestCase):
''' Main class for add testing; Can be added to a suite'''
# The function "setUp" will always be ran in order to setup the
# test environment before all the tests have run.
def setUp(self):
'''Verify environment is setup properly''' # Printed if test fails
pass
# The function "tearDown" will always be ran in order to cleanup the
# test environment after all the tests have run.
def tearDown(self):
'''Verify environment is tore down properly''' # Printed if test fails
pass
# Functions beginning with "test" will be ran as a unit test.
def test_positive_add(self):
'''Verify that adding positive numbers works''' # Printed if test fails
self.assertEqual(add(10,23,22), 55) # Test will fail if "false" (boolean)
self.assertEqual(add(11,23), 34)
self.assertEqual(add(1,1,17), 19)
# Functions beginning with "test" will be ran as a unit test.
# @unittest.skip("demonstrating skipping") # Skip this test (Python >= 2.7)
def test_negative_add(self):
'''Verify that adding negative numbers works''' # Printed if test fails
self.assertEqual(add(-12,23), 11)
# Functions beginning with "test" will be ran as a unit test.
# In this case, this test will be skipped.
#@unittest.skip("demonstrating skipping")
def test_negative_add_skip(self):
'''Verify that adding negative numbers works''' # Printed if test fails
self.assertEqual(add(-12,23), 11)
if __name__=='__main__':
unittest.main()

For more complicated test suites and modularity, please take a look at the git repository I’ve setup on bitbucket.org. Download the source, read the README file, and you’ll be able to have a working example of a testsuite (run with python pyunit_intro/suite/test/unittest_run_all.py).