all Python 3unittestfunction
unittest.TestCase.setUp()
fixtures
Run before EVERY test method of the class to prepare state (files, DB, mocks); an exception in setUp is an error, a failed assert* a failure; the test is skipped.
Syntax
def setUp(self): ... # до каждого теста
setUp deliberately runs again before every single test to keep them isolated; if the preparation is expensive (starting a server, opening a DB connection), move it to setUpClass, which runs once per class. For releasing resources, calling self.addCleanup(...) right after you acquire something is safer than tearDown — cleanups still fire when setUp blows up halfway through, while tearDown is skipped entirely.
Examples
import unittest
class DBTest(unittest.TestCase):
def setUp(self):
self.data = {'key': 'val'} # инициализация
def test_key(self):
self.assertIn('key', self.data)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy