[TIP] both naming a parameter and showing its values

Brianna Laugher brianna.laugher at gmail.com
Mon Mar 23 16:34:54 PDT 2015


Hi dpb,

Is it that you would prefer the name of each argument, as well as its
value, always appears in each test id?

Basically, if you want to use a different format for making your test
ids and have it easily apply for all your tests, yes, that is fairly
easy. You can make your own decorator which calls
pytest.mark.parametrize and does the id forming as you want it to.

But if you just want to do this for one test case, then I think what
you have written is already the most succinct way.

I put an example function (and example output) here:
https://gist.github.com/pfctdayelise/86c8a577b60afb174178

Hope I have understood you correctly.

cheers
Brianna


On 23 March 2015 at 14:43, dpb dpb <dpb.mediamath at gmail.com> wrote:
> Sorry, the last piece of code I posted had errors making it unreadable. It
> should be:
>
>> import pytest
>>
>> def params(n):
>>     fn = range(n)
>>     return {'name': 'a', 'args': fn, 'ids': ['a: {}'.format(i) for i in
>> fn]}
>>
>> named_args = params(3)
>>
>> @pytest.mark.parametrize(
>>         named_args['name'], named_args['args'], ids=named_args['ids'])
>> def test_funcs_with_naming_func(a):
>>     assert a ** 3 == a * (a ** 2)
>
>
>
> - dpb
>
> On Sun, Mar 22, 2015 at 10:53 PM, dpb dpb <dpb.mediamath at gmail.com> wrote:
>>
>> The way the ids parameter of parametrize currently works, we have the
>> choice either to show the actual argument values, one by one, or else a
>> string assigned to parameters as a whole:
>>
>>> import pytest
>>>
>>> @pytest.mark.parametrize('a', range(3))
>>> def test_funcs_without_name(a):
>>>     assert a ** 3 == a * (a ** 2)
>>>
>>>
>>> @pytest.mark.parametrize('a', range(3), ids=['value of a'] * 3)
>>> def test_funcs_with_name(a):
>>>     assert a ** 3 == a * (a ** 2)
>>>
>>> $ py.test params/demo_ids.py -v
>>> ============================= test session starts
>>> ==============================
>>> platform darwin -- Python 3.4.1 -- py-1.4.25 -- pytest-2.6.3 -- ...
>>> collected 6 items
>>> params/demo_ids.py::test_funcs_without_name[0] PASSED
>>> params/demo_ids.py::test_funcs_without_name[1] PASSED
>>> params/demo_ids.py::test_funcs_without_name[2] PASSED
>>> params/demo_ids.py::test_funcs_with_name[value of a] PASSED
>>> params/demo_ids.py::test_funcs_with_name[value of a] PASSED
>>> params/demo_ids.py::test_funcs_with_name[value of a] PASSED
>>> =========================== 6 passed in 0.02 seconds
>>> ===========================
>>> $
>>
>>
>> If I want to show both the value of each argument and its name, I can do
>> something like this:
>>
>>> def fn(n):
>>>     return range(n)
>>>
>>> @pytest.mark.parametrize('a', fn(3), ids=['a: {}'.format(i) for i in
>>> fn(3)])
>>> def test_funcs_with_naming_func(a):
>>>     assert a ** 3 == a * (a ** 2)
>>>
>>>
>>> params/demo_ids.py::test_funcs_with_naming_func[a: 0] PASSED
>>> params/demo_ids.py::test_funcs_with_naming_func[a: 1] PASSED
>>> params/demo_ids.py::test_funcs_with_naming_func[a: 2] PASSED
>>
>>
>> I can see to it that neither the argnames nor the argvalues appear more
>> than once in the set of parameters to pytest.markparametrize.
>>
>>> def params(n):
>>>
>>>     fn = range(n)
>>>
>>>     return {{'name': 'a', 'args': fn, 'ids': ['a: {}'.format(i) for i in
>>> fn]}
>>>
>>>
>>> named_args = params(3)
>>>
>>>
>>> @pytest.mark.parametrize(named_args['name'], pairs['args'],
>>> ids=pairs['ids'])
>>> def test_funcs_with_naming_func(a):
>>>     assert a ** 3 == a * (a ** 2)
>>
>>
>> But I wonder if there is a more compact way to accomplish this within
>> Pytest itself, without the separate function
>>
>> Thanks for your time.
>>
>> - dpb
>>
>>
>
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>



-- 
They've just been waiting in a mountain for the right moment:
http://modernthings.org/



More information about the testing-in-python mailing list