当前位置:网站首页>[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 .
Introduce to you item Less explicit parameters in :
- keywords: The current use case is
markThe 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 .
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 .
边栏推荐
- Is the securities account given by qiniu business school safe? Do you charge for opening an account
- leetcode463. Perimeter of the island (simple)
- 2022年危险化学品经营单位主要负责人考试模拟100题及模拟考试
- pycharm和anaconda的基础上解决Jupyter连接不上Kernel(内核)的问题--解决方案1
- leetcode684. Redundant connection (medium)
- Exception handling and exception usage in golang
- Characteristics of interfaces & comparison between interfaces and abstract classes
- GemBox.Bundle 43.0 Crack
- Project failed to load the configuration file of Nacos configuration center
- Intranet penetration based on UDP port guessing
猜你喜欢

The micro service failed to connect to the cloud sentinel console and the link blank problem occurred after the connection was successful (resolved)

2022年危险化学品经营单位主要负责人考试模拟100题及模拟考试

基于文本驱动用于创建和编辑图像(附源代码)

Redis - learn five types of NoSQL

【pytest学习】pytest 用例执行失败后其他不再执行

leetcode463. Perimeter of the island (simple)

2022 national question bank and mock examination for safety officer-b certificate

LeetCode——42. 接雨水(双指针)

Differences between list and set access elements

Pychart tips - how to set up a background picture
随机推荐
2022安全员-A证考试题模拟考试题库模拟考试平台操作
2022 molten welding and thermal cutting work license and simulation examination
Elasitcsearch基础学习笔记(1)
Leetcode 1974. Minimum time to type words using a special typewriter (yes, once)
“is-a”,“has-a”,“like-a”
web安全-靶场笔记
Learn about Prometheus from 0 to 1
Pytest test framework Basics
核密度估计(二维、三维)
unittest 如何知道每个测试用例的执行时间
2022 high voltage electrician special operation certificate examination question bank and online simulation examination
Global and Chinese molten carbonate fuel cell industry outlook and market panoramic Research Report 2022-2028
485天,我远程办公的 21 条心得分享|社区征文
2022年R1快開門式壓力容器操作考試題庫及模擬考試
Kernel density estimation (2D, 3D)
JVM 的组成
2022 safety officer-a certificate test question simulation test question bank simulation test platform operation
solr(一)solr的安装及权限控制
(validation file) validatejarfile report errors
【pytest学习】pytest 用例执行失败后其他不再执行