Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pytest-如何知道选择了哪些标记_Python_Python 3.x_Pytest - Fatal编程技术网

Python pytest-如何知道选择了哪些标记

Python pytest-如何知道选择了哪些标记,python,python-3.x,pytest,Python,Python 3.x,Pytest,pytest中是否有任何方法可以知道从命令行中选择了哪些标记 我有一些测试标记为“慢”,需要进行大量处理。我只想在标记慢激活的情况下进行治疗 heavy\u var=None def设置_模块(模块): 全球重风险 #这里需要帮助!? 如果标记[“慢”]: 重型车辆=治疗() def test_simple(): 通过 @pytest.mark.slow() def test_slow(): 断言重变量x==“…” 如何知道是否选择了慢速标记?当我使用-m not slow标记[“slow”]

pytest中是否有任何方法可以知道从命令行中选择了哪些标记

我有一些测试标记为“慢”,需要进行大量处理。我只想在标记慢激活的情况下进行治疗

heavy\u var=None
def设置_模块(模块):
全球重风险
#这里需要帮助!?
如果标记[“慢”]:
重型车辆=治疗()
def test_simple():
通过
@pytest.mark.slow()
def test_slow():
断言重变量x==“…”

如何知道是否选择了慢速标记?当我使用
-m not slow
标记[“slow”]
调用pytest时,如果仅在选择了标记为
slow
的测试时才需要运行某些代码,则可以通过过滤模块范围的夹具中的测试项来完成,该夹具将替换
设置模块。例如:

@pytest.fixture(scope='module', autouse=True)
def init_heavy_var(request):
    for item in request.session.items:
        if item.get_closest_marker('slow') is not None:
            # found a test marked with the 'slow' marker, invoke heavy lifting
            heavy_var = treatment()
            break

不是真的-不是来自
设置\u模块
,它是为了与
nose
和xUnit兼容,而且非常有限;不过,您可以将其替换为等效的夹具。而不是标记列表/dict-只有您从命令行输入的字符串,例如,您可以访问
not slow
字符串。因此,我想要做的唯一方法是使用sys.argv?
sys.argv
也只会给您
-m
字符串arg,因此您必须自己进行解析。如果无法使用pytest进行解析(不解析argv),是否只有在某些标记被激活的情况下,才有其他干净的方法来处理治疗?哦,当然,这是可能的-我太专注于问题标题了。让我简单地给你一个例子。