Search code examples
djangodjango-testingdjango-tests

Does django testing have a breakDown() similar to setUp() that is ran after all tests complete?


I am running some tests in django but they depend on a response from an outside service. For instance, I might create a customer and wish to acknowledge it has been created in the outside service.

Once I am done with testing I want to remove any test customers from the outside service.

Ideally, there would be a method similar to setUp() that runs after all tests have completed. Does anything like this exist?


Solution

  • You can make use of either unittest.TestCase.tearDown or unittest.TestCase.tearDownClass

    tearDown(...) is the method gets called immediately after the test method has been called and the result recorded.

    but, the tearDownClass(...) is gets called after tests in an individual class have run. That is, once per test class.

    IMO, using tearDownClass(...) method is more appropriate since you may not need to check/acknoledge the external service after search test cases of the same class