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 the setUp() 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 ...

Get Mastering Object-oriented Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.