1

Need to run same test on different devices. Used fixture to give ip addresses of the devices, and all tests run for the IPs provided by fixtures as requests. But at the same time, need to append the test name with the IP address to quickly analyze results. pytest results have test name as same for all params, only in the log or statement we could see the parameter used, is there anyway to change the testname by appending the param to the test name based on the fixture params ?

class TestClass:

    def test1():
       pass

    def test2():
       pass

We need to run the whole test class for every device, all test methods in sequence for each device. We can not run each test with paramter cycle, we need to run the whole test class in a parameter cycle. This we achieved by a fixture implementation, but we couldn't rename the tests.

MortenB
  • 2,749
  • 1
  • 31
  • 35
karthik
  • 31
  • 1
  • 4

2 Answers2

2

You can read my answer: How to customize the pytest name

I could change the pytest name, by creating a hook in a conftest.py file. However, I had to use pytest private variables, so my solution could stop working when you upgrade pytest

Gelineau
  • 2,031
  • 4
  • 20
  • 30
0

You don't need to change the test name. The use case you're describing is exactly what parametrized fixtures are for.

Per the pytest docs, here's output from an example test run. Notice how the fixture values are included in the failure output right after the name of the test. This makes it obvious which test cases are failing.

$ pytest
======= test session starts ========
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
rootdir: $REGENDOC_TMPDIR, inifile:
collected 3 items

test_expectation.py ..F

======= FAILURES ========
_______ test_eval[6*9-42] ________

test_input = '6*9', expected = 42

    @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
E       AssertionError: assert 54 == 42
E        +  where 54 = eval('6*9')

test_expectation.py:8: AssertionError
======= 1 failed, 2 passed in 0.12 seconds ========
Frank T
  • 8,268
  • 8
  • 50
  • 67
  • I modified the question, clarifying the exact scenario, Do you have any suggestions ? – karthik Jul 27 '17 at 22:39
  • You may want to check answer of [this question](https://stackoverflow.com/questions/37182929/pytest-parameters-execution-order-for-repeated-test-seems-to-be-wrong) Perhaps it might help – Macintosh_89 Jun 10 '18 at 11:10