How to add marks dynamically #11743
-
In pytest, I attempted to dynamically add @pytest.mark.xxx to test cases using @env_mark. However, it doesn't take effect on test cases decorated with pytest.mark.parametrize. Below is a brief example of the test case code: class TestOrder(HttpBaseTestCase):
@env_mark(["dev", "test"])
@pytest.mark.parametrize('order_id', ['123', '456'])
def test_do_get_order_id(self, order_id):
pass Here is the simplified @env_mark code: def env_mark(env):
def _env_mark(func):
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
env_wrapper = [_mark_map[i] for i in env]
for _env_wrapper in env_wrapper:
wrapper = _env_wrapper(wrapper)
return functools.wraps(func)(wrapper)
return _env_mark
_mark_map = {
"dev": pytest.mark.dev,
"test": pytest.mark.,
"prod": pytest.mark.prod
and so on...
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Please stop creating all those wrappers Instead just directly apply the Marks |
Beta Was this translation helpful? Give feedback.
-
So I have a non-wrapper related need for this: I am doing combinatoric testing across a number of parameters. Only some of those combinations are valid. I would like to provide the parameterization as a fixture so that i can repeat the parameterization across a number of tests. I want to mark the tests according to one of the parameters. The closest I can come is something like this: from itertools import product
cases = itertools.product([1,2,3], ['a', 'b', 'c'])
@pytest.fixture(
params = [
pytest.param('x', marks=pytest.mark.x),
pytest.param('y', marks=pytest.mark.y),
pytest.param('z', marks=pytest.mark.y)
]
)
def other_cases(request):
return request.param
@pytest.fixture(params=cases)
def combinatoric_fixture(other_cases, request):
# assume this can be done
case = merge(other_cases, request.param)
if case.skip():
pytest.skip()
return case That allows me to mark the tests correctly, but it produces a ton of skips, and since this is a product over a large number of cases that ends up burning a decent amount of time because there is a ~small amount of work done when iterating to merge the results of the product. I have tried using It would actually be really great to be able to apply multiple marks for each of the items in the product, but i will settle for just one. Is there some way of doing this that i'm missing? |
Beta Was this translation helpful? Give feedback.
Please stop creating all those wrappers
Instead just directly apply the Marks