[TIP] both naming a parameter and showing its values

dpb dpb dpb.mediamath at gmail.com
Mon Mar 23 06:43:17 PDT 2015


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
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20150323/4f316de0/attachment.html>


More information about the testing-in-python mailing list