next up previous contents
Next: Bibliography Up: 14 Appendices Previous: 14 Appendices   Contents


14.1 PyUnit

PyUnit is a testing framework for the programming language Python. It will be used mainly in the writing of unit and integration tests, but may also be used during other testing phases.

A PyUnit test is made up of a class that inherits from unittest.TestCase. These tests are combined into test suites which are derived from the unittest.TestSuite class.

Once a Test Suite has been created, a runner object, of type unittest.TextTestRunner is created to actually run all the tests in the suite. The runner outputs the results of the test suite. Information on PyUnit can be found here: http://pyunit.sourceforge.net/

The template to be used for PyUnit files is as follows:

#
# File: UTTemplate.py
#
# vim: ts=4 sw=4 noexpandtab:
#
from time import localtime, strftime
import sys

sys.path.append("../../../sam/")

import unittest
import someclass

hline = "-------------------------------------------------------------\n"

class someclassTestCase(unittest.TestCase):
	"""One line summary of the class (this must be on one line!!).
	Purpose:    A more detailed description of the class and its
				intended behaviour.
	Uses:       Class1, Class2
	Author:     login
	Date:       13/5/02
	"""
	# Setup Function
	def setUp(self):
		"""Purpose:        Sets up X for testing by doing
						blah and doing blah. 
		Uses:           Class2 
		Preconditions:  Assumptions about the input
		Postconditions: Assumptions about the output
		Author:         login
		Date:           13/5/02
		"""
		sys.stderr.write(hline)
		sys.stderr.write("setUp finished\n")
		return
	
	def Test1(self):
		"""Name:		Test1
		Purpose:        What the test is expected to test
		Uses:           Class2 
		Preconditions:  Assumptions about the input 
		Postconditions: Assumptions about the output 
		Author:         login 
		Date:           13/5/02
		"""
		sys.stderr.write(hline)
		sys.stderr.write(self.Test1.__doc__)
		# testing & assert goes here
		sys.stderr.write("Test case Passed\n")
		return

# The code to run the Test Suite

# print header
sys.stderr.write(hline)
sys.stderr.write("Unit Test Report for Someclass\n")
sys.stderr.write(someclassTestCase.__doc__)
sys.stderr.write(strftime("\n%a, %d %b %Y %H:%M:%S +0000\n", localtime()))
sys.stderr.write(hline)

# Set up the suite
someclassTestSuite = unittest.TestSuite()
someclassTestSuite.addTest(someclassTestCase("Test1"))
# Run the suite
runner = unittest.TextTestRunner()
runner.run(someclassTestSuite)