Using setup and teardown
There are three levels of setup and teardown available for the unittest
modules. Here are the three different kinds of testing scopes: method, class, and module.
Test case setUp() and tearDown() methods: These methods ensure that each individual test method within a
TestCase
class has had a proper setup and teardown. Often, we'll use thesetUp()
method to create the unit objects and any mock objects that are required. We don't want to do something costly such as creating whole databases, as these methods are used before and after each test method.Test case setUpClass() and tearDownClass() methods: These methods perform a one-time setup (and teardown) around all the tests in a
TestCase
class. These methods bracket the sequence ofsetUp()-testMethod()-tearDown()
for each method. This can be a good place to create and destroy the test data or a test schema inside a database.Module setUpModule() and tearDownModule() functions: These standalone functions provide us...