skip and xfail
preface
In actual use, it is often necessary to skip some test cases. pytest provides skip and ifskip to skip the test
skip
Tag in test function
Usage example: @ pytest mark. Skip (reason = "the reason for skipping will be printed in the execution result"), for example 🌰:
import pytest def test_1(): print("Test case 1") @pytest.mark.skip(reason="If it is not finished, this use case will not be executed") def test_2(): print("Test case 2")
The results are as follows:
Tag in the test case of the test class
For example 🌰:
import pytest class TestCase(object): def test_1(self): print("Test case 1") @pytest.mark.skip(reason="If it is not finished, this use case will not be executed") def test_2(self): print("Test case 2")
The results are as follows:
Mark on test class
For example 🌰:
import pytest @pytest.mark.skip(reason="If it is not finished, this use case will not be executed") class TestCase1(object): def test_1(self): print("Test case 1") def test_2(self): print("Test case 2") class TestCase2(object): def test_3(self): print("Test case 3") def test_4(self): print("Test case 4")
The results are as follows:
summary
- @pytest.mark.skip can be added to functions, classes and class methods
- If it is added to the class, all test cases in the class will not be executed
Skip test cases at module level
Syntax: pytest Skip (MSG = "", allow_module_level = false), which should be distinguished from decorator usage
When allow_ module_ When level = true, you can set to skip the entire module at the module level
For example 🌰:
import pytest pytest.skip("Skip entire module", allow_module_level=True) class TestCase1(object): def test_1(self): print("Test case 1") def test_2(self): print("Test case 2") class TestCase2(object): def test_3(self): print("Test case 3") def test_4(self): print("Test case 4") def test_5(): print('Execute test case 5')
The results are as follows:
pytestmark skip module
Use the pytestmark variable and make it equal to the label.
import pytest pytestmark = pytest.mark.skip(reason="Skip entire module") class TestCase1(object): def test_1(self): print("Test case 1") def test_2(self): print("Test case 2")
The results are as follows:
Note: using the pytestmark variable to skip the module is equivalent to changing all case s under the module and adding @ pytest mark. Skip () decorator, need and pytest Skip () method distinction
skipif
Consistent with skip usage, it is used to conditionally skip some use cases
**Note: * * condition needs to return True to skip
Syntax: @ pytest mark. skipif(condition, reason="")
For example 🌰:
import sys import pytest @pytest.mark.skipif(sys.platform == 'win32', reason="does not run on win32") class TestSkipIf(object): def test_demo(self): print("Not in win32 Run on")
The results are as follows:
Skip the use of marked variables
- You can set pytest mark. Skip and pytest mark. Skipif is assigned to a tag variable
- This tag variable is shared between different modules
- If the test cases of multiple modules need to use the same skip or skipif, you can use a separate file to manage these common tags, and then apply to the whole test case set
For example 🌰:
import sys import pytest skipmark = pytest.mark.skip(reason="Do not execute this use case") skipifmark = pytest.mark.skipif(sys.platform == 'win32', reason="does not run on win32") @skipifmark class TestSkipIf(object): def test_demo(self): print("Not in MacOS Run on") @skipmark def test_1(): print("Test case 1") def test_2(): print("Test case 2")
The results are as follows:
importorskip
Skip use cases when some imports are missing
Syntax:
pytest.importorskip( modname: str, minversion: Optional[str] = None, reason: Optional[str] = None )
Parameters:
- modname: the name of the module to be imported, such as selenium;
- minversion: indicates the minimum version number to be imported. If the version fails to meet the standard, an error message will be printed;
- reason: given this parameter, the given message content will be displayed only when the module is not imported
For example 🌰, Import module that does not exist:
def test_1(): print(pytest.importorskip("selenium-haha"))
The results are as follows:
Import a module that exists but has the wrong version
import pytest sel = pytest.importorskip("selenium", minversion="3.150") @sel def test_1(): print("Test whether it is imported selenium modular")
The results are as follows:
xfail
Failed to mark test function / class
Syntax:
@pytest.mark.xfail(condition = None, reason = None, raises = None, run = True, strict = False)
Parameters:
- Condition: the condition for the expected failure. Parameters are required
- Reason: the reason for the failure. Parameters are required
- strict: if xpass occurs, the result of the test suite is regarded as a failure
For example 🌰, Mark test function and class as failed:
import sys import pytest @pytest.mark.xfail(sys.platform == "win32", reason="no reason") class TestCase1(object): def test_1(self): print("Test case 1") def test_2(self): print("Test case 2") class TestCase2(object): def test_3(self): print("Test case 3") def test_4(self): print("Test case 4") @pytest.mark.xfail(reason="no reason") def test_5(): print('Execute test case 5')
The results are as follows:
Use of strict parameter
import sys import pytest @pytest.mark.xfail(sys.platform == "win32", reason="no reason") class TestCase1(object): def test_1(self): print("Test case 1") def test_2(self): print("Test case 2") class TestCase2(object): def test_3(self): print("Test case 3") def test_4(self): print("Test case 4") @pytest.mark.xfail(reason="no reason", strict=True) def test_5(): print('Execute test case 5')
The results are as follows:
The strict parameter can also use xfail_ Configure the strict variable to pytest INI file
[pytest] xfail_strict = true
Ignore xfail
By specifying on the command line:
pytest --runxfail
You can force a test to run and report xfail tags as if it had no tags at all, which results in pytest Xfail has no effect