首页 > 解决方案 > 如何访问测试脚本之外的标记列表

问题描述

可以说,我有一个标记,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.pyand实现test_1.py,所以不能使用夹具。

还有许多其他带有跳过条件的标记(specific_case_2 = pytest.mark.skipif(<CONDITION_2>), specific_case_3 = pytest.mark.skipif(<CONDITION_3>),...),所以我不能只使用if 'skipif' in _marks解决方案

标签: pythonpytest

解决方案


由于您的module.py访问pytest标记,因此可以安全地假设它是测试代码的一部分。

话虽如此,如果您愿意将类属性更改A.valuepytest固定装置,那么这种替代解决方案可能对您有用。否则,这还不够。

替代解决方案

不要使用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

推荐阅读