How do I disable a test using pytest?

前端 未结 6 1941
终归单人心
终归单人心 2020-12-13 23:49

Let\'s say I have a bunch of tests:

def test_func_one():
    ...

def test_func_two():
    ...

def test_func_three():
    ...

Is there a dec

6条回答
  •  清歌不尽
    2020-12-14 00:17

    You can mark a test with the skip and skipif decorators when you want to skip a test in pytest.

    Skipping a test

    @pytest.mark.skip(reason="no way of currently testing this")
    def test_func_one():
        ...
    

    The simplest way to skip a test is to mark it with the skip decorator which may be passed an optional reason.

    It is also possible to skip imperatively during test execution or setup by calling the pytest.skip(reason) function. This is useful when it is not possible to evaluate the skip condition during import time.

    def test_func_one():
        if not valid_config():
            pytest.skip("unsupported configuration")
    

    Skipping a test based on a condition

    @pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6 or higher")
    def test_func_one():
        ...
    

    If you want to skip based on a conditional then you can use skipif instead. In the previous example, the test function is skipped when run on an interpreter earlier than Python3.6.

    Finally, if you want to skip a test because you are sure it is failing, you might also consider using the xfail marker to indicate that you expect a test to fail.

提交回复
热议问题