all Python 3unittestfunction
unittest.TestCase.tearDown()
fixtures
Run after EVERY test to release what setUp acquired; it still runs when the test fails, but only if setUp itself completed successfully.
Syntax
def tearDown(self): ... # после каждого теста
tearDown is one method for all resources: if the first release line raises, the rest never run and a file or connection is left dangling. Registering self.addCleanup(...) as soon as each resource is acquired is sturdier — cleanups run in reverse order, one failing does not cancel the others, and they fire even when setUp never finished. An exception raised inside tearDown itself marks the test as an error even if every assertion passed.
Examples
import unittest
class DBTest(unittest.TestCase):
def setUp(self):
self.data = {'key': 'val'}
def tearDown(self):
self.data.clear() # очистка
See also
Open in the full glossarydocs.python.orgReport an inaccuracy