微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在python中有条件地跳过测试

我希望在满足条件时跳过一些测试函数,例如:

@skip_unless(condition)
def test_method(self):
    ...

在这里,如果条件评估为true,我希望将测试方法报告为跳过.我能用鼻子做一些努力,但我想看看是否有可能在鼻子2.

Related question描述了在nose2中跳过所有测试的方法.

解决方法:

通用解决方案:

你可以使用unittest跳过条件,它可以用于nosetests,nose2和pytest.有两种选择:

class TestTheTest(unittest.TestCase):
    @unittest.skipIf(condition, reason)
    def test_that_runs_when_condition_false(self):
        assert 1 == 1

    @unittest.skipUnless(condition, reason)
    def test_that_runs_when_condition_true(self):
        assert 1 == 1

Pytest

使用pytest框架:

@pytest.mark.skipif(condition, reason)
def test_that_runs_when_condition_false():
    assert 1 == 1

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐