First page Back Continue Last page Graphics
Literate Testing:
Automated Testing with doctest
Jim Fulton, jim@zope.com
Tim Peters, tim@zope.com
PyCon 2004
Notes:
Unit testing is an important practice for improving the quality of software and for enabling refactoring. The standard Python unit testing framework, PyUnit, was based on the existing Java unit testing framework, JUnit. The framework provides a testing applications programming interface (API) through inheritance that supports test set up and provides various ways of making assertions about tests. It's common for testing code to contain more testing API calls than application code. Descriptive text is provided as comments and is usually in short supply.
Doctest is a system for writing tests within Python documentation strings. The emphasis is on documentation. Tests are provided as example code, set off with Python prompts. Doctest tests lend themselves toward a literate form of test code.
In Python 2.3, a new feature was added to Python to create unit tests from doctest doc strings. This talk provides an overview of doctest, shows how to create unit tests with doctest, and compares and contrasts regular PyUnit unit tests and doctest-based unit tests.
Tim Peters is the original author of doctest, and has been active in core Python development since Python 0.9.1.
Jim Fulton is the chief architect of Zope and the Zope object database, and has been a Python contributor since 1994.