python - 如何访问测试脚本之外的标记列表
问题描述
可以说,我有一个标记,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
测试功能之外的确切列表吗?
PS我还找到了一些关于如何使用夹具获取标记列表的提示,但我应该坚持当前的module.py
and实现test_1.py
,所以不能使用夹具。
还有许多其他带有跳过条件的标记(specific_case_2 = pytest.mark.skipif(<CONDITION_2>)
, specific_case_3 = pytest.mark.skipif(<CONDITION_3>)
,...),所以我不能只使用if 'skipif' in _marks
解决方案
解决方案
由于您的module.py
访问pytest
标记,因此可以安全地假设它是测试代码的一部分。
话虽如此,如果您愿意将类属性更改A.value
为pytest
固定装置,那么这种替代解决方案可能对您有用。否则,这还不够。
替代解决方案
不要使用pytest.mark._markers
来检索标记列表,而是使用request.keywords。
类 FixtureRequest
关键词
- 底层节点的关键字/标记字典。
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
推荐阅读
- arrays - 如何使用不同的类将数组从一种方法转移到另一种方法?
- java - 如何在 if 语句中使用 Optional?
- reactjs - 如何在 webpack.config.js 中配置 @babel/plugin-proposal-class-properties?
- javascript - react-scripts build:无法缩小此文件中的代码:opentok.js:1310
- performance - 在直方图算法中使用 ++ 增加八度的更快方法
- java - 爪哇 | 具有相同方法的接口和类的多重继承
- c++ - 为什么在 C++ 中的类中拥有一个线程并使用构造函数调用它如此困难?
- c# - 使用 UserManager.IsInRoleAsync() 时出现 InvalidOperationException
- reactjs - React-Intl 更改语言不会更改翻译
- php - 如何将日期固定为类似的格式?