Pytest hides some parameters values

Viewed 191

when running pytest, I can see each test with its parameters values. eg:

import pytest

@pytest.mark.parametrize("a", [1])
def test_foo(a):
    pass

On terminal I can see the value 1:

plugins: metadata-1.8.0, timeout-1.3.3, html-2.0.1
collected 1 item                                                               
<Module a_tests.py>
  <Function test_foo[1]>

The problem is, that on more complicated value types (like list) pytest hides the value:

import pytest

@pytest.mark.parametrize("a", [[1,2,3]])
def test_foo(a):
    pass

plugins: metadata-1.8.0, timeout-1.3.3, html-2.0.1
collected 1 item                                                               
<Module a_tests.py>
  <Function test_foo[a0]>

How can I see the values of the list?

2 Answers

You can use the ids argument to customize the name of parameterized test functions. The easiest way in your example is just to use str to get a string representation of the parameter:

import pytest

@pytest.mark.parametrize("a", [[1,2,3], [4, 5, 6]], ids=str)
def test_foo(a):
    pass

This gives:

test.py::test_foo[[1, 2, 3]] 
test.py::test_foo[[4, 5, 6]] 

For more complicated names you can use a function that takes the parameter as argument. Here is a simple example:

def param_name(param):
    if isinstance(param, list):
        return '-'.join([str(el) for el in param])
    return str(param)

@pytest.mark.parametrize("a", [[1,2,3], [4,5,6]], ids=param_name)
def test_foo(a):
    pass

This gives:

test.py::test_foo[1-2-3] 
test.py::test_foo[4-5-6] 

Take a look at Pycharm or another IDE for a more user friendly printing of pytest results.

Related