繁体   English   中英

在测试中通过参数化访问 Pytest 标记

[英]Access Pytest marks from parametrize in the test

我有大量使用@pytest.mark.parametrize和相当大的自定义标记集的测试。 我想不出一种从测试中访问这些标记的方法。 文档解释了如何从conftest.py文件中执行此操作,而不是从测试函数中。

我真的不需要对标记进行操作,我只需要注册它们。

pytest.ini

[pytest]
markers =
    MarkFoo
    MarkBar

test.py

import pytest

from typing import Any
from dataclasses import dataclass

@dataclass(frozen=True)
class FooTest:
    name: str                           # Unique test name
    param: int                          # A parameter
    marks: Any = ()                     # Marks for this test


test_list = [
    FooTest(name='Foo', param=1, marks=(pytest.mark.MarkFoo)),
    FooTest(name='Bar', param=2, marks=(pytest.mark.MarkBar)),
]

@pytest.mark.parametrize( "name, param, ",
    [ pytest.param(t.name, t.param, marks=t.marks) for t in test_list ]
)

def test_run_foo_bar(name, param, record_property):
    # How to read the marks here?
    # record_property("marks:", ???)
    print(name, param)    

如何从测试中获取分数? 谢谢!

事实证明,如果您使用固定装置,这将非常简单:

@pytest.fixture()
def test_marks(request):
    marks = ["MarkFoo", "MarkBar"]
    return [m for m in request.node.own_markers if m.name in marks]

def test_run_foo_bar(name, param, record_property, test_marks):
    record_property("Test Marks:", test_marks)
    print(name, param, test_marks)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM