python - 从单元测试套件中删除测试

标签 python unit-testing

为了运行我的所有单元测试,我使用以下脚本,其中 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/

相关文章:

python - 将有趣的特殊拉丁字符转换为 unicode(外来字符)

python - Flask - 如何使用 RotatingFileHandler 将 werkzeug 日志写入日志文件?

python - 如何用 2 位数字排序数字字典键

php - 像这样的功能应该有单元测试吗?

java - 在测试和待测方法中使用相同的值

asp.net-mvc - 你能模拟/ stub 内部属性吗?

python - 获取需要用户交互的页面

python - 如何在 Ubuntu 10.04 上安装 python 包

java - 如何使用 Junit 模拟 Builder

Clojure 中的单元测试副作用