pytest test framework (6) --- use skip and skipif to skip test cases

table of Contents

1. Introduction

2. How to use

1. @pytest.mark.skip decorator

2, pytest.skip method

[email protected] decorator

4. pytestmark variables

5, conftest.py

Three, execution method


1. Introduction

I recently encountered a scenario when writing automation use cases. Some use cases are only for the CentOS operating system, and other systems need to skip them. In this case, skip, skipif, etc. can be used to achieve the function of skipping test cases.

 

2. How to use

1. @pytest.mark.skip decorator

Add the @pytest.mark.skip decorator in front of the use case or class and pass in the reason to skip the use case or class. The parameters are as follows:

pytest.mark.skip(*, reason=None)

※  reason (str) – Reason why the test function is being skipped.

@pytest.mark.skip(reason="This feature has not yet been implemented") 
def test_aaa(): 
    ……

 

2, pytest.skip method

The pytest.skip(reason) method is called during the test execution process, using the given message to skip the use case being executed:

skip(msg[, allow_module_level=False])

※  allow_module_level (bool) – Allows this function to be called at module level, skipping the rest of the module. Defaults to False.

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

When allow_moudel_level=True, the entire module will be skipped:

if not pytest.config.getoption("--custom-flag"):
    pytest.skip("--custom-flag is missing, skipping tests", allow_module_level=True)

 

[email protected] decorator

Add the @pytest.mark.skipif decorator in front of the use case or class to conditionally skip the use case or class:

pytest.mark.skipif(condition, *, reason=None)

※  condition (bool or str) – True/False if the condition should be skipped or a condition string.

※  reason (str) – Reason why the test function is being skipped.

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

 

4. pytestmark variables

Define the pytestmark variable, this variable will act on the entire module:

import pytest

pytestmark = pytest.mark.skip('作用于模块中的每一个用例,所以test_ccc、test_ddd都将跳过')

def test_ccc():
    assert True

def test_ddd():
    assert True

operation result:

>>>pytest test.py
========================================================== test session starts =============================================================
platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: D:\6-0-Prepare_Job\TestCase
plugins: allure-pytest-2.8.18, html-2.1.1, metadata-1.10.0
collected 2 items                                                                                                                           

test.py ss                                                                                                                            [100%]

========================================================== 2 skipped in 0.02s =============================================================

 

5, conftest.py

Configure the collect_ignore_glob item in conftest.py to skip the specified files and directories during the collection phase of the use case. For example, skip the files in the current test directory whose file names match the test_*.py rule and the files in the subfolder fff of eee:

collect_ignore_glob = ['test*.py', 'eee/fff']

 

Three, execution method

By default, pytest does not display the detailed information of skip and xfail use cases, we can make it display through the -r option. Usually a letter represents a type, the specific rules are:

(f)ailed, (E)rror, (s)kipped, (x)failed, (X)passed, (p)assed, (P)assed with output, (a)ll except passed(p/P), or (A)ll

Therefore, the display result is a use case of SKIPPED:

pytest -rs

 

 

Guess you like

Origin blog.csdn.net/wxt_hillwill/article/details/114868359