Published on (
 See this if you're having trouble printing code examples

Python News

Extreme Python


Also in Python News:

BitTorrent Style

Twisted Python

Python Escapes Classroom

Humongous Python

Py in Print

Guido van Rossum announced the second beta of Python 2.1 last week. There are only a few minor changes from the first beta. One notable addition is Steve Purcell's unit testing program, PyUnit. PyUnit is a Python version of Kent Beck and Erich Gamma's Java unit testing program, JUnit. Adding PyUnit to Python core will allow more Python programmers to explore Extreme Programming methods.

Extreme Programming (or XP) is a set of 12 programming practices for rapid application development for small development teams. The rapid nature of the development has a natural appeal to Python programmers. Unit testing is one of the 12 XP practices. Unit testing is essentially testing each chunk or "unit" of code you write. You write the tests as you write the code. Actually, you should write the tests before the code. You define the behavior you want from each class, write a test for that behavior, and then write the code. When you manage to run the tests without error, you know you have accomplished what you set out to do. Incremental testing as you code builds a battery of tests that can be used to ensure later changes to your code do not violate the behavior you have defined. It also changes the way you think about the code. You learn to define and stick to interface specifications.

Zope developer Lalo Martins has created a Zope wrapper to PyUnit, ZUnit. Lalo proposes integrating unit testing into Zope's product class, which will make it easier to run tests directly from the Zope control panel. Currently, Zope ships with PyUnit.

PyUnit is not the only unit testing framework in the Standard library for Python 2.1. Tim Peter's was added in the first beta. This module provides a very easy way to write a test. You simply embed some example code that demonstrates the behavior you expect into your class documentation, and then you add some magic testing lines to the end of your code and doctest will seek out your examples and test them. With a little cut and paste from an interactive session you have a test to verify that your code continues to live up to your documented interface.

Both testing frameworks were probably added as a response to Jeremy Hylton's prompting back in December.

Stephen Figgins administrates Linux servers for Sunflower Broadband, a cable company.

Read more Python News columns.

Copyright © 2009 O'Reilly Media, Inc.