Naggappan Ramukannan
Naggappan Ramukannan

Reputation: 2812

Dynamic pytest method generation with only custom name

Hi I am trying the followign example mentioned in pytest doc,

# content of test_expectation.py
@pytest.mark.parametrize("test_input,expected", [
("3+5", 8),
("2+4", 6),
("6*9", 42),
])
def test_eval(test_input, expected):
    assert eval(test_input) == expected

When i run using py.test -v it gives me the output as below,

test_code.py::test_eval[3+5-8] PASSED
test_code.py::test_eval[2+4-6] PASSED
test_code.py::test_eval[6*9-42] FAILED

Here when I generate html reports the names are too long when I use very long input data.

In above example lest take 1st result the name of method is [3+5-8]. i.e its taken the tuple (3+5,8) and attached it to the actual test case method name.

Now In my case the tuple is ("short name", "very long string") instead of (3+5,8) so In my html report is showing very long. Is it possible to show only the "short name" and not the 2nd value?

Upvotes: 1

Views: 339

Answers (1)

Sanju
Sanju

Reputation: 2194

could you please paste the current displayed long name and your expected short name ,so that your question becomes clear. Just in case if it helps , you can make use "ids" field in @pytest.mark.parametrize to customise displayed test name with parametrized values.

for e,g

@pytest.mark.parametrize("test_input,expected", [
("3+5", 8), 
("2+4", 6),
("6*9", 42),
], ids=['cust_name_1', 'cust_name_2', 'cust_name_3'])
def test_eval(test_input, expected):
...

would display your test name as

test_code.py::test_eval[cust_name_1] PASSED
test_code.py::test_eval[cust_name_1] PASSED
test_code.py::test_eval[cust_name_1] FAILED

Upvotes: 2

Related Questions