跳过没有装饰器语法的单元测试测试 [英] Skip unittest test without decorator syntax

查看:24
本文介绍了跳过没有装饰器语法的单元测试测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一套使用 TestLoader(来自 unittest 模块)loadTestsFromModule() 方法加载的测试,即

I have a suite of tests that I have loaded using TestLoader's (from the unittest module) loadTestsFromModule() method, i.e.,

suite = loader.loadTestsFromModule(module)

这给了我一个非常充足的测试列表,可以正常工作.我的问题是我正在使用的测试工具有时需要根据各种标准跳过某些测试.我想做的是这样的:

This gives me a perfectly ample list of tests that works fine. My problem is that the test harness I'm working with sometimes needs to skip certain tests based on various criteria. What I want to do is something like this:

for test in suite:
    mark the test as 'to-skip' if it meets certain criteria

请注意,我不能只是从测试列表中删除测试,因为我希望 unittest 测试运行器实际上跳过测试,将它们添加到跳过的计数中,以及所有这些爵士乐.

Note that I can't just remove the test from the list of tests because I want the unittest test runner to actually skip the tests, add them to the skipped count, and all of that jazz.

unittest 文档建议在测试方法或类周围使用装饰器.由于我是从模块加载这些测试并根据测试本身未包含的标准决定跳过,因此我无法真正使用装饰器.有没有一种方法可以迭代每个单独的测试,以及如何将其标记为跳过"测试而无需直接访问测试类或类中的方法?

The unittest documentation suggests using decorators around the test methods or classes. Since I'm loading these tests from a module and determining to skip based on criteria not contained within the tests themselves, I can't really use decorators. Is there a way I can iterate over each individual test and some how mark it as a "to-skip" test without having to directly access the test class or methods within the class?

推荐答案

使用 unittest.TestCase.skipTest:

import unittest

class TestFoo(unittest.TestCase):
    def setUp(self): print('setup')
    def tearDown(self): print('teardown')
    def test_spam(self): pass
    def test_egg(self): pass
    def test_ham(self): pass

if __name__ == '__main__':
    import sys
    loader = unittest.loader.defaultTestLoader
    runner = unittest.TextTestRunner(verbosity=2)
    suite = loader.loadTestsFromModule(sys.modules['__main__'])
    for ts in suite:
        for t in ts:
            if t.id().endswith('am'): # To skip `test_spam` and `test_ham`
                setattr(t, 'setUp', lambda: t.skipTest('criteria'))
    runner.run(suite)

印刷品

test_egg (__main__.TestFoo) ... setup
teardown
ok
test_ham (__main__.TestFoo) ... skipped 'criteria'
test_spam (__main__.TestFoo) ... skipped 'criteria'

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK (skipped=2)


----------------------------------------------------------------------
Ran 3 tests in 0.002s

OK (skipped=2)

更新

更新代码以修补 setUp 而不是测试方法.否则,将执行 setUp/tearDown 方法以跳过测试.

Updated the code to patch setUp instead of test method. Otherwise, setUp/tearDown methods will be executed for test to be skipped.

注意

unittest.TestCase.skipTest(测试跳过)是在 Python 2.7, 3.1 中引入的.所以这个方法只适用于Python 2.7+, 3.1+.

unittest.TestCase.skipTest (Test skipping) was introduced in Python 2.7, 3.1. So this method only work in Python 2.7+, 3.1+.

这篇关于跳过没有装饰器语法的单元测试测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆