11

I would like to skip some test functions when a condition is met, for example:

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

Here I expect the test method to be reported as skipped if condition evaluated to true. I was able to do this with some effort with nose, but I would like to see if it is possible in nose2.

Related question describes a method for skipping all tests in nose2.

4

3 回答 3

15

通用解决方案:

您可以使用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
于 2017-12-08T09:52:31.937 回答
4

内置unittest.skipUnless()方法,它应该与鼻子一起工作:

于 2016-04-04T15:59:01.887 回答
1

使用鼻子:

#1.py
from nose import SkipTest

class worker:
    def __init__(self):
        self.skip_condition = False

class TestBench:
    @classmethod
    def setUpClass(cls):
        cls.core = worker()
    def setup(self):
        print "setup", self.core.skip_condition
    def test_1(self):
        self.core.skip_condition = True
        assert True
    def test_2(self):
        if self.core.skip_condition:
            raise SkipTest("Skipping this test")

鼻子测试 -v --nocapture 1.py

1.TestBench.test_1 ... setup False
ok
1.TestBench.test_2 ... setup True
SKIP: Skipping this test

----------------------------------------------------------------------
XML: /home/aladin/audio_subsystem_tests/nosetests.xml
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK (SKIP=1)
于 2019-11-13T16:09:02.610 回答