pytest检测是否排除标记

2024-09-30 06:18:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经参数化了测试,这些测试从fixture中获取输入,如下面的代码片段所示。这些固定装置只能在特定的条件下工作,这些条件与是否设置了特定的pytest标记有关。你知道吗

我的问题是:如何在PytestConfig的定义中询问某个标记是否存在,以便跳过PytestConfig中的某些部分,我知道如果排除某个标记,这些部分将失败?你知道吗

我想让下面的代码片段适用于pytest -m 'not markertobeskipped'

import os

import pytest


class PytestConfig:
    # here I'd like to throw in some conditional logic along the lines of:
    # "if pytest is called with 'not markertobeskipped': ...
    missing_config = os.getenv('SOME_CONFIGURATION_NOT_PRESENT_IF_MARKERTOBESKIPPED', None)
    # here, in reality some longish loading process would happen
    assert missing_config

    @staticmethod
    def get_input_data():
        return []


class TestClass:
    @pytest.mark.markertobeskipped
    @pytest.mark.parametrize('input', PytestConfig.get_input_data())
    def test_should_be_skipped_without_error(self, input):
        assert input

Tags: 代码in标记importinputherepytestos

热门问题