Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply custom markers to specific value of pytest parameterized marker

Tags:

python

pytest

import pytest

@pytest.mark.parametrize('a', [0, 1])
def test_increment(a):
    pass    

I want to apply custom-marker to a specific value of a marker. In the above-mentioned test case to be should be executed with

  • a=0 if -m one is provided.
  • a=1 if -m two is provided.

I tried the following snippet, but it doesn't work.

import pytest

@pytest.mark.parametrize('a', [pytest.mark.one(0), pytest.mark.two(1)])
def test_increment(a):
    pass    
like image 439
Jay Joshi Avatar asked Nov 18 '25 06:11

Jay Joshi


1 Answers

I was able to provide custom marker to a parametrized test using pytest.param.

Document link: https://docs.pytest.org/en/latest/reference.html#pytest-param

Example: http://doc.pytest.org/en/latest/example/parametrize.html#set-marks-or-test-id-for-individual-parametrized-test

Snippet

@pytest.mark.parametrize('a', [pytest.param(0, marks=pytest.mark.one), pytest.param(1, marks=pytest.mark.two)])
def test_increment(a):
    pass    
like image 80
Jay Joshi Avatar answered Nov 20 '25 20:11

Jay Joshi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!