为了运行我的所有单元测试,我使用以下脚本,其中 test_files 是我的测试文件的字符串列表:
for test_file in test_files:
test_file = test_file[:-3]
module = __import__(test_file)
for name, obj in inspect.getmembers(module):
if inspect.isclass(obj):
if str(obj).startswith("<class 'test_"):
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(obj))
之后如何从套件中删除单个测试(不是测试文件中的所有测试)?
最佳答案
我最终创建了一个新套件并添加了除我想跳过的测试之外的所有测试。为了将测试列为已跳过,我创建了一个虚拟的 SkipCase 类。
class SkipCase(unittest.TestCase):
def runTest(self):
raise unittest.SkipTest("Test would take to long.")
new_suite = unittest.TestSuite()
blacklist = [
'test_some_test_that_should_be_skipped',
'test_another_test_that_should_be_skipped'
]
for test_group in suite._tests:
for test in test_group:
if test._testMethodName in blacklist:
testName = test._testMethodName
setattr(test, testName, getattr(SkipCase(), 'runTest'))
new_suite.addTest(test)
关于python - 从单元测试套件中删除测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12141170/