简体   繁体   中英

How to set dynamic marks parameter in pytest.param when using pytest_generate_tests()

I have a pytest setup where all of my tests are parameterized through data stored in a YAML file. Included in that YAML file are custom markers for various test instances. I stripped out most of the code for simplicity. The problem is the variable marker below needs to be of type _pytest.mark.structures.MarkDecorator

I can't seem to change the object type from string. I'm thinking there must be a simpler/different way for what I'm trying to do but I'm just not seeing it...

def pytest_generate_tests(metafunc):

    ######################################################################
    # A bunch of code removed for simplicity but cm is defined in yaml file
    ######################################################################
    cm = "custom_marker"
    marker = f"pytest.mark.{cm}"
    final_str = pytest.param((1,2), marks=marker)

This doesn't directly answer your question, but I wrote a library called parametrize_from_file for the exact purpose parametrizing tests (in pytest) from YAML/TOML/JSON/NT files. It supports custom marks, and might be a lot easier than writing your own parametrization code.

Posting answer based on Kale's response. Thank you Kale I needed to change this:

cm = "custom_marker"
    marker = f"pytest.mark.{cm}"
    final_str = pytest.param((1,2), marks=marker)

to this:

cm = "custom_marker"
    marker = getattr(pytest.mark, cm)
    final_str = pytest.param((1,2), marks=marker)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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