SetUp functions for multiple test cases

Duncan Booth duncan.booth at invalid.invalid
Tue Jan 20 10:45:52 EST 2009


Roy Smith <roy at panix.com> wrote:

> You might have your setUp() method re-assign the global to an instance
> variable and then your test cases can access it via self.whatever. 
> The reason for that is if at some point in the future you change your
> mind and decide to re-build the database in setUp() for each test, you
> just have to change setUp(), not touch the individual test cases.

Or you could use a class attribute to save the database connection.

Set it up the first time the test runs but on subsequent tests just use the 
saved values. If you have tests spread across separate files you could even 
set up the database connection in a base class and so it only gets created 
once across any number of test suites.

This has the advantage that your test will work even if not invoked as the 
main script. Putting the setup code inside name=='__main__' will break as 
soon as you have multiple test files.

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list