6.4. A Regression Test Script
As we've seen, Python provides interfaces to a variety of system services, along with tools for adding others. Example 6-5 shows some commonly used services in action. It implements a simple regression-test system by running a command-line program with a set of given input files and comparing the output of each run to the prior run's results. This script was adapted from an automated testing system I wrote to catch errors introduced by changes in program source files; in a big system, you might not know when a fix is really a bug in disguise.
Example 6-5. PP3E\System\Filetools\regtest.py
Some of this script is Unix biased. For instance, the 2>&1 syntax to redirect stderr works on Unix but not on all flavors of Windows, and the diff command line spawned is a Unix utility (cmp does similar wok on Windows). You'll need to tweak such code a bit to run this script on certain platforms.
Also, given the improvements to the os module's popen calls as of Python 2.0, these calls have now become a more portable way to redirect streams in such a script and an alternative to shell command redirection syntax (see the subprocess module mentioned near the end of the prior chapter for another way to control process streams).
But this script's basic operation is straightforward: for each filename with an .in suffix in the test directory, this script runs the program named on the command line and looks for deviations in its results. This is an easy way to spot changes (called regressions) in the behavior of programs spawned from the shell. The real secret of this script's success is in the filenames used to record test information; within a given test directory testdir:
Output and difference files are generated in the test directory with distinct suffixes. For example, if we have an executable program or script called shrubbery and a test directory called test1 containing a set of .in input files, a typical run of the tester might look something like this:
% regtest.py shrubbery test1 RegTest start. user: mark path: /home/mark/stuff/python/testing time: Mon Feb 26 21:13:20 1996 FAILED: test1/t1.in (see test1/t1.in.diffs) PASSED: test1/t2.in FAILED: test1/t3.in (see test1/t3.in.diffs) RegTest done: Mon Feb 26 21:13:27 1996
Here, shrubbery is run three times for the three .in canned input files, and the results of each run are compared to output generated for these three inputs the last time testing was conducted. Such a Python script might be launched once a day to automatically spot deviations caused by recent source code changes (e.g., from a cron job on Unix).
We've already met system interfaces used by this script; most are fairly standard Unix calls, and they are not very Python specific. In fact, much of what happens when we run this script occurs in programs spawned by os.system calls. This script is really just a driver; because it is completely independent of both the program to be tested and the inputs it will read, we can add new test cases on the fly by dropping a new input file in a test directory.
So given that this script just drives other programs with standard Unix-like calls, why use Python here instead of something like C? First, the equivalent program in C would be much longer: it would need to declare variables, handle data structures, and more. In C, all external services exist in a single global scope (the linker's scope); in Python, they are partitioned into module namespaces (os, sys, etc.) to avoid name clashes. And unlike C, the Python code can be run immediately, without compiling and linking; changes can be tested much quicker in Python. Moreover, with just a little extra work, we could make this script run on Windows too. As you can probably tell by now, Python excels when it comes to portability and productivity.
Because of such benefits, automated testing is a very common role for Python scripts. If you are interested in using Python for testing, be sure to see Python's web site (http://www.python.org) for other available tools. In particular, the PyUnit (a.k.a. unittest) and doctest standard library modules provide testing frameworks for Python programmers. In a nutshell, here's what each does:
See the Python library manual for more details and the Vaults of Parnassus and PyPI web sites for additional testing toolkits in the third-party domain.