pytest检测标记是否被排除

时间:2018-11-20 11:23:52

标签: python pytest

我有参数化测试,这些测试从夹具中获取输入,如以下代码片段所示。这些灯具只能在与是否设置某个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

0 个答案:

没有答案