16.3.9 测试固件
固件是测试所需的外部资源。例如,一个类的测试可能都需要另一个类的示例(用来提供配置设置)或者另一个共享资源。其他测试固件包括数据库连接和临时文件。(很多人可能对此有争议,认为使用外部资源会使这些测试不再是“单元”测试,不过它们仍是测试,而且仍然很有用)。
unittest包括一个特殊的hook,用来配置和清理测试所需的所有固件。要为各个单独的测试用例建立固件,需要覆盖TestCase上的setup()。要完成清理,则要覆盖tearDown()。要为一个测试类的所有实例管理一组固件,则需要覆盖TestCase的类方法setUpClass()和tearDownClass()。最后如果要为一个模块中的所有测试处理代价特别昂贵的建立操作则可以使用模块级函数setUpModule()和tearDownModule()。
import random
import unittest
def setUpModule():
print('In setUpModule()')
def tearDownModule():
print('In tearDownModule()')
class FixturesTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
print('In setUpClass()')
cls.good_range = range(1,10)
@classmethod
def tearDownClass(cls):
print('In tearDownClass()')
del cls.good_range
def setUp(self):
super().setUp()
print('\nIn setUp()')
# Pick a number sure to be in the range. The range is
# defined as not including the "stop" value, so this
# value should not be included in the set of allowed
# values for our choice.
self.value = random.randint(
self.good_range.start,
self.good_range.stop - 1,
)
def tearDown(self):
print('In tearDown()')
del self.value
super().tearDown()
def test1(self):
print('In test1()')
self.assertIn(self.value,self.good_range)
def test2(self):
print('In test2()')
self.assertIn(self.value,self.good_range)
运行这个示例测试时,固件和测试方法执行的顺序很明显。
如果清理固件的过程中出现错误,那么tearDown方法可能不会都被调用。为了确保总能正确地释放固件,要使用addCleanup()。
import random
import shutil
import tempfile
import unittest
def remove_tmpdir(dirname):
print('in remove_tmpdir()')
shutil.rmtree(dirname)
class FixturesTest(unittest.TestCase):
def setUp(self):
super().setUp()
self.tmpdir = tempfile.mkdtemp()
self.addCleanup(remove_tmpdir,self.tmpdir)
def test1(self):
print('\nIn test1()')
def test2(self):
print('\nIn test2()')
这个示例测试会创建一个临时目录,然后在测试完成时使用shutil清理这个目录。