2

In order to run all my unittests I use following script where test_files is a list of strings of my testfiles:

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))

How can I remove single tests from the suite afterwards (not all tests from a testfile)?

4

2 回答 2

3

我最终创建了一个新套件并添加了所有测试,除了我想跳过的那些。为了将测试列为已跳过,我创建了一个虚拟 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)
于 2012-09-05T11:51:02.493 回答
2

您可以在类或方法的基础上使用此跳过装饰器:

import unittest
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
class MarketTest(unittest.TestCase):
    def setUp(self):
        return
    @unittest.skip("Skipping market basics test")
    def test_market_libraries(self):
        return
于 2015-10-21T16:49:25.667 回答