Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override a pytest parameterized functions name

My parameters determine the name of my parameterized pytest. I will be using a some randomized params for these tests. In order for my reporting names in junit to not get messed up, I'd like to create a static name for each parameterized test.

Is it possible?

JUnit seems to have a parameter: Changing names of parameterized tests

class TestMe:
    @pytest.mark.parametrize(
        ("testname", "op", "value"),
        [
            ("testA", "plus", "3"),
            ("testB", "minus", "1"),
        ]
    )
    def test_ops(self, testname, op, value):

I tried overwriting request.node.name however I can only rename it during test execution.

I'm almost positive I either need to write a plugin or a fixture. What do you think would be the best way to go about this?

like image 467
SomeGuyOnAComputer Avatar asked Jun 01 '16 17:06

SomeGuyOnAComputer


People also ask

What is the syntax to run a parameterized test in pytest?

you can put @pytest. mark. parametrize style parametrization on the test functions to parametrize input/output values as well.

Can pytest fixtures be parameterized?

pytest. fixture() allows one to parametrize fixture functions.

What is pytest Mark Parametrize do?

The @pytest. mark. parametrize() decorator lets you parameterize arguments of the testing function independent of fixtures you created.

How do you run the same test multiple times in pytest?

Repeating a test Each test collected by pytest will be run count times. If you want to override default tests executions order, you can use --repeat-scope command line option with one of the next values: session , module , class or function (default). It behaves like a scope of the pytest fixture.


1 Answers

You're looking for the ids argument of pytest.mark.parametrize:

list of string ids, or a callable. If strings, each is corresponding to the argvalues so that they are part of the test id. If callable, it should take one argument (a single argvalue) and return a string or return None.

Your code would look like

@pytest.mark.parametrize(
    ("testname", "op", "value"),
    [
        ("testA", "plus", "3"),
        ("testB", "minus", "1"),
    ],
    ids=['testA id', 'testB id']
)
def test_industry(self, testname, op, value):
like image 70
vaultah Avatar answered Sep 22 '22 18:09

vaultah