As of Python 2.7, the unittest framework introduces setUpClass and tearDownClass, which allow you to execute setup and teardown operations for the entire test class, rather than before and after each individual test.
These functions are defined with the @classmethod decorator and are executed only once for the class.
Example:
class TestSequenceFunctions(unittest.TestCase):
@classmethod
def setUpClass(cls):
start() # Launched at the beginning, once for the class
@classmethod
def tearDownClass(cls):
end() # Launched at the end, once for the class
def test_choice(self):
element = random.choice(self.seq)
self.assertTrue(element in self.seq)
def test_sample(self):
with self.assertRaises(ValueError):
random.sample(self.seq, 20)
for element in random.sample(self.seq, 5):
self.assertTrue(element in self.seq)