- publishing free software manuals
An Introduction to Python
by Guido van Rossum and Fred L. Drake, Jr.
Paperback (6"x9"), 124 pages
ISBN 0954161769
RRP £12.95 ($19.95)

Sales of this book support the Python Software Foundation! Get a printed copy>>>

10.11 Quality Control

One approach for developing high quality software is to write tests for each function as it is developed and to run those tests frequently during the development process.

The ‘doctest’ module provides a tool for scanning a module and validating tests embedded in a program's docstrings. Test construction is as simple as cutting-and-pasting a typical call along with its results into the docstring. This improves the documentation by providing the user with an example and it allows the doctest module to make sure the code remains true to the documentation:

    def average(values):
        """Computes the arithmetic mean of a list of numbers.
        >>> print average([20, 30, 70])
        return sum(values, 0.0) / len(values)
    import doctest
    doctest.testmod()   # automatically validate the tests

The ‘unittest’ module is not as effortless as the ‘doctest’ module, but it allows a more comprehensive set of tests to be maintained in a separate file:

    import unittest
    class TestStatisticalFunctions(unittest.TestCase):
        def test_average(self):
            self.assertEqual(average([20, 30, 70]), 40.0)
            self.assertEqual(round(average([1, 5, 7]), 1), 4.3)
            self.assertRaises(ZeroDivisionError, average, [])
            self.assertRaises(TypeError, average, 20, 30, 70)
    unittest.main() # Calling from the command line invokes
                    # all tests
ISBN 0954161769An Introduction to PythonSee the print edition