gpt4 book ai didi

python - 如何在测试脚本之外访问标记列表

转载 作者:行者123 更新时间:2023-12-04 16:45:41 25 4
gpt4 key购买 nike

我有一个标记,比如说,specific_case = pytest.mark.skipif(<CONDITION>)我需要将其应用于一些测试用例。我要房产value在应用标记的情况下返回不同的值。这是我的简化代码:
模块.py :

import pytest

class A():
@property
def value(self):
_marks = pytest.mark._markers # current code to get applied marks list
if 'specific_case' in _marks:
return 1
else:
return 2
test_1.py :
import pytest
from module import A

pytestmark = [pytest.mark.test_id.TC_1, pytest.mark.specific_case]

def test_1():
a = A()
assert a.value == 1
但这不适用于 pytest.mark._markers返回 set(['TC_1', 'skipif'])但不准确 pytestmark列表(我希望 set(['TC_1', 'specific_case']) 或至少 pytestmark 原样 - [pytest.mark.test_id.TC_1, pytest.mark.specific_case] )。
那么有什么方法可以访问准确的 pytestmark列出测试功能之外的内容?
附言我还发现了一些关于如何使用 fixture 获取标记列表的提示,但我应该坚持当前的 module.py 实现。和 test_1.py ,所以不能使用fixture。
还有许多其他带有跳过条件的标记( specific_case_2 = pytest.mark.skipif(<CONDITION_2>)specific_case_3 = pytest.mark.skipif(<CONDITION_3>) ,...),所以我不能只使用 if 'skipif' in _marks解决方案

最佳答案

由于您的 module.py访问 pytest标记,那么可以安全地假设它是测试代码的一部分。
话虽如此,如果您愿意更改类属性 A.valuepytest固定装置,那么此替代解决方案可能适合您。否则,这还不够。
替代方案
而不是使用 pytest.mark._markers要检索标记列表,请使用 request.keywords .

class FixtureRequest

keywords

  • Keywords/markers dictionary for the underlying node.
import pytest


# Data

class A():
@property
def value(self):
_marks = pytest.mark._markers # Current code to get applied marks list
print("Using class property A.value:", list(_marks))
if 'specific_case' in _marks:
return 1
else:
return 2


@pytest.fixture
def a_value(request): # This fixture can be in conftest.py so all test files can see it. Or use pytest_plugins to include the file containing this.
_marks = request.keywords # Alternative style of getting applied marks list
print("Using pytest fixture a_value:", list(_marks))
if 'specific_case' in _marks:
return 1
else:
return 2


# Tests

pytestmark = [pytest.mark.test_id, pytest.mark.specific_case]

def test_first():
a = A()
assert a.value != 1 # 'specific_case' was not recognized as a marker


def test_second(a_value):
assert a_value == 1 # 'specific_case' was recognized as a marker
输出:
pytest -q -rP --disable-pytest-warnings
.. [100%]
================================================================================================= PASSES ==================================================================================================
_______________________________________________________________________________________________ test_first ________________________________________________________________________________________________
------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------
Using class property A.value: ['parametrize', 'skipif', 'skip', 'trylast', 'filterwarnings', 'tryfirst', 'usefixtures', 'xfail']
_______________________________________________________________________________________________ test_second _______________________________________________________________________________________________
------------------------------------------------------------------------------------------ Captured stdout setup ------------------------------------------------------------------------------------------
Using pytest fixture a_value: ['specific_case', '2', 'test_1.py', 'test_second', 'test_id']
2 passed, 2 warnings in 0.01s

关于python - 如何在测试脚本之外访问标记列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68590303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com