一、给Unittest测试类添加参数
调用unittest.main()时无法传入额外的参数给我们的测试类时,可以使用如下工具给我们的测试类添加额外的参数:
unittest_utils.py代码:
import unittest
class ParametrizedTestCase(unittest.TestCase):
""" TestCase classes that want to be parametrized should
inherit from this class.
"""
def __init__(self, method_name='runTest', param=None):
super(ParametrizedTestCase, self).__init__(method_name)
self.param = param
@staticmethod
def parametrize(test_case_class, param=None):
""" Create a suite containing all tests taken from the given
subclass, passing them the parameter 'param'.
"""
test_loader = unittest.TestLoader()
test_names = test_loader.getTestCaseNames(test_case_class)
suite = unittest.TestSuite()
for name in test_names:
suite