Executing this script isn't much use. First, the test must be collected in a testsuite, and the testsuite must then be run. A testsuite is a collection of related tests, of the type TestSuite.
It is customary to create a function suite() in every testfile that return a suite with all the tests the file contains. This function is then called by a TestRunner object. This will be either a TextTestRunner, if you run your tests without a gui, or a gui TestRunner application, such as the QtTestRunner. Defining a testsuite is a simple matter of creating a TestSuite object and adding tests:
def suite(): testSuite=unittest.TestSuite() testSuite.addTest(DocviewDocTestCase()) return testSuite
In order to be able to execute this file it's handy to add a bit of executable code to the end:
def main(): runner = unittest.TextTestRunner() runner.run(suite()) if __name__=="__main__": main()
Running the test will give you the satisfaction of knowing that at least one piece of your application is working:
boud@calcifer:~/doc/pyqt > python ch9/dvt1.py runTest (__main__.DocviewDocTestCase) ... ok ------------------------------------------------------------------------------ Ran 1 test in 0.035s OK
Or, if you want to see the green and pleasant bar, you can run the gui testrunner. Enter the name of the testmodule (dvt1 in this case) in the textbox, followed by the name of the function that returns the testsuite:
Preparing a GUI test run.
Then press run, and sit back. Please note that the actual output you get might differ. The python unittesting framework is in constant development. For the screen output in this chapter, I used version 1.3.0, which is included with the other sources that belong to this book. The unittest gui has been brought up to date to the version of unittest.py that's included with Python 2.2.