[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

doctest
Parses out and reruns tests from an interactive session log that is pasted into a
module’s docstrings. The logs give test calls and expected results; doctest essen-
tially reruns the interactive session.


See the Python library manual, the PyPI website, and your favorite Web search engine
for additional testing toolkits in both Python itself and the third-party domain.


For automated testing of Python command-line scripts that run as independent pro-
grams and tap into standard script execution context, though, our tester does the job.
Because the test driver is fully independent of the scripts it tests, we can drop in new
test cases without having to update the driver’s code. And because it is written in Py-
thon, it’s quick and easy to change as our testing needs evolve. As we’ll see again in the
next section, this “scriptability” that Python provides can be a decided advantage for
real tasks.


Testing Gone Bad?
Once we learn about sending email from Python scripts in Chapter 13, you might also
want to augment this script to automatically send out email when regularly run tests
fail (e.g., when run from a cron job on Unix). That way, you don’t even need to re-
member to check results. Of course, you could go further still.
One company I worked for added sound effects to compiler test scripts; you got an
audible round of applause if no regressions were found and an entirely different noise
otherwise. (See playfile.py at the end of this chapter for hints.)
Another company in my development past ran a nightly test script that automatically
isolated the source code file check-in that triggered a test regression and sent a nasty
email to the guilty party (and his or her supervisor). Nobody expects the Spanish
Inquisition!

Copying Directory Trees


My CD writer sometimes does weird things. In fact, copies of files with odd names can
be totally botched on the CD, even though other files show up in one piece. That’s not
necessarily a showstopper; if just a few files are trashed in a big CD backup copy, I can
always copy the offending files elsewhere one at a time. Unfortunately, drag-and-drop
copies on some versions of Windows don’t play nicely with such a CD: the copy op-
eration stops and exits the moment the first bad file is encountered. You get only as
many files as were copied up to the error, but no more.


In fact, this is not limited to CD copies. I’ve run into similar problems when trying to
back up my laptop’s hard drive to another drive—the drag-and-drop copy stops with
an error as soon as it reaches a file with a name that is too long or odd to copy (common


304 | Chapter 6: Complete System Programs

Free download pdf