Chapter 9. PyUnit

Overview

PyUnit brings xUnit to Python. Python is an interpreted, interactive, object-oriented programming language, widely used for many different kinds of software development. Steve Purcell ported JUnit to Python to create PyUnit. It follows the generic xUnit model closely. The standard Python libraries have included PyUnit since Python 2.1.

Python is open source software that is copyrighted but freely usable and distributable. PyUnit has the same terms as Python itself, with a stipulation that Steve Purcell be credited as the author in the source code and any accompanying documentation.

For more information about Python, see http://www.python.org. PyUnit’s home is http://pyunit.sourceforge.net. The information in this chapter is based on Python 2.3.3, which includes Version 1.4.6 of PyUnit.

Architecture

PyUnit’s architecture is simple and effective. Tests are implemented by inheritance from a base class, TestCase , which supports test fixture behavior. Tests may be aggregated using a TestSuite . A number of test assert methods are provided. The PyUnit module, unittest.py , not only contains the foundation code for building unit tests, but also acts as a test runner to execute tests from the command line. A GUI test runner is also provided.

Usage

Test classes are created by subclassing TestCase. The simplest approach is to override the method runTest( ) , as shown in Example 9-1.

Example 9-1. Simple unit test for the class Book
booktests.py """Unit test for book.py""" ...

Get Unit Test Frameworks now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.