当前位置:网站首页>[pytest learning] after the pytest case fails to execute, the others will not be executed

[pytest learning] after the pytest case fails to execute, the others will not be executed

2022-06-11 16:52:00 qq_ thirty-seven million six hundred and sixty-eight thousand f

1.hook Function introduction

When executing use cases , There is an association before the use case is encountered , After the use case execution fails , The rest of the use cases don't need to be executed ( such as : After login failure , My list 、 There is no need to execute my order and other use cases ).

To complete this function, we just need to learn pytest Give us two Hook function :

  • pytest_runtest_setup : Execute... Before executing the test , similar unittest Medium setup Method .
  • pytest_runtest_makereport: Test report hook function , Print information about the test run .

2.pytest_runtest_setup

Method has only one parameter item, No manual transfer is required ,pytest The required content will be automatically passed into this method .
item: It mainly records the name of the use case 、 Module 、 The class name 、 File path and other information .
 Insert picture description here
Introduce to you item Less explicit parameters in :

  • keywords: The current use case is mark The name of the tag .
    - for example @pytest.mark.somke:keywords == somke
  • cls: The class name of the current use case .

3.pytest_runtest_makereport

Method has two parameters item and call,item The parameters are the same as above .
Parameters call : Used to record the execution process 、 Test report and other information .
 Insert picture description here
call Introduction to parameters in :

  • when: Record the running status of the test case , There are three states in total :
    - setup: Use cases “ Before execution ”.
    - call: Use cases " In execution ".
    - teardown: Use cases " Execution completed ".
  • excinfo: If the use case execution fails , It will be recorded here .

4. Solutions

Through the above analysis , You can get a simple idea , Namely : Before each test case is executed , Judge whether the class of the current use case has a failed use case , Use if you have pytest.xfail Use cases marked as failed , No more execution .

step :
1. Define a global variable .
2. Make one of your own mark Mark , Mark the use cases with coupling between use cases ( You can use this to control whether you need ).
3. Add failure case : use pytest_runtest_makereport hook Use in a function call Medium excinfo Information determines whether the use case fails , If it fails, add the use case to the global variable .
4. Determine whether there is a failed use case before use case execution : use pytest_runtest_setuphook Function to determine whether the class of the current use case has a failed use case .

Implementation code :
The current code is better in conftest.py Implementation in file .

# conftest.py

from typing import Dict, Tuple
import pytest

#  Global variables , Record failed use cases 
_test_failed_incremental: Dict[str, Dict[Tuple[int, ...], str]] = {
    }


def pytest_runtest_makereport(item, call):
    #  Judge whether to skip after case execution fails 
    if "incremental" in item.keywords:
    	#  If the use case fails , Add to global variables 
        if call.excinfo is not None:
            cls_name = str(item.cls)
            parametrize_index = (
                tuple(item.callspec.indices.values())
                if hasattr(item, "callspec")
                else ()
            )
            test_name = item.originalname or item.name
            _test_failed_incremental.setdefault(cls_name, {
    }).setdefault(
                parametrize_index, test_name
            )


def pytest_runtest_setup(item):
	#  Judge whether to skip after case execution fails 
    if "incremental" in item.keywords:
        cls_name = str(item.cls)
        #  Determine whether the class of the current use case is in the global variable 
        if cls_name in _test_failed_incremental:
            parametrize_index = (
                tuple(item.callspec.indices.values())
                if hasattr(item, "callspec")
                else ()
            )
            test_name = _test_failed_incremental[cls_name].get(parametrize_index, None)
            #  If the use case in the current class has a failed use case , Just skip. 
            if test_name is not None:
                pytest.xfail("previous test failed ({})".format(test_name))

Test code :

@pytest.mark.incremental
class TestAdd:

    def test_01(self):
        print('test_01  Use case execution ...')

    
    def test_02(self):
        pytest.xfail(' Subsequent use cases have failed 0')

    def test_03(self):
        print('test_03  Use case execution ...')
        
if __name__ == "__main__":
    pytest.main()

result :
test_01 Use case executed successfully ,
test_02 Failure ,
test_03 skip .
 Insert picture description here

原网站

版权声明
本文为[qq_ thirty-seven million six hundred and sixty-eight thousand f]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111620589790.html