python自动化测试脚本中,测试用例被不同的marker name装饰,那么如果获取这些marker name呢?

1.测试文件:

tests/

  --conftest.py

  --test_demo.py

 

 

2.代码实现

test_demo.py

import pytest

class TestDemo:

    @pytest.mark.TEST_12345
    def test_demo_1(self, fix_1):
        print("demo 1 pass")
        assert True

 

conftest.py

import pytest

@pytest.fixture(scope="function", autouse=True)
def record_test_id(request, record_property):
    """
    Record property: id
    This function will be automatically run for every test case
    """
    markers_list = list(request.node.iter_markers())
    print("marker_list:", markers_list)
    if len(markers_list) >= 1:
        record_property(
            "id",
            [marker.name.replace("_", "-") for marker in markers_list if "TEST" in marker.name],
        )


@pytest.fixture
def fix_1():
    """fixture"""

    print("\n------------")
    fun_test()

def fun_test():
    """function"""

    print("***********")

results: