How to make pytest display a custom string representation for fixture parameters?
Posted
by
Björn Pollex
on Stack Overflow
See other posts from Stack Overflow
or by Björn Pollex
Published on 2014-05-28T21:23:37Z
Indexed on
2014/05/28
21:25 UTC
Read the original article
Hit count: 188
When using builtin types as fixture parameters, pytest prints out the value of the parameters in the test report. For example:
@fixture(params=['hello', 'world']
def data(request):
return request.param
def test_something(data):
pass
Running this with py.test --verbose
will print something like:
test_example.py:7: test_something[hello]
PASSED
test_example.py:7: test_something[world]
PASSED
Note that the value of the parameter is printed in square brackets after the test name.
Now, when using an object of a user-defined class as parameter, like so:
class Param(object):
def __init__(self, text):
self.text = text
@fixture(params=[Param('hello'), Param('world')]
def data(request):
return request.param
def test_something(data):
pass
pytest will simply enumerate the number of values (p0
, p1
, etc.):
test_example.py:7: test_something[p0]
PASSED
test_example.py:7: test_something[p1]
PASSED
This behavior does not change even when the user-defined class provides custom __str__
and __repr__
implementations. Is there any way to make pytest display something more useful than just p0
here?
I am using pytest 2.5.2 on Python 2.7.6 on Windows 7.
© Stack Overflow or respective owner