addCleanup vs tearDown
Per the addCleanup
doc string:
Cleanup items are called even if setUp fails (unlike tearDown)
addCleanup
can be used to register multiple functions, so you could use
separate functions for each resource you wish to clean up. That would allow your
code to be a bit more reusable/modular.
addCleanup()
methods will run even if one of them fails, and will run even if setUp()
fails. You should also consider using pytest.
Another good thing about addCleanup
is that it just works as you'd expect.
For example, if you call it in a setUp
function, then all test methods will call the cleanup function in the end.
If you call it in a test method, only this method calls the cleanup function.