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

Viewed 717

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?

2 Answers

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

So Django's testing framework uses a Python standard library module, unittest. This is where the setUp() method comes from.

This library contains another method tearDown() that is called immediately after the tests are run. More info can be found here

Related