<div dir="ltr"><div>Sorry, the last piece of code I posted had errors making it unreadable. It should be:<br><div><br></div><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><div><span style="font-family:monospace,monospace">import pytest<br><br>def params(n):<br>    fn = range(n)<br>    return {&#39;name&#39;: &#39;a&#39;, &#39;args&#39;: fn, &#39;ids&#39;: [&#39;a: {}&#39;.format(i) for i in fn]}<br><br>named_args = params(3)<br><br>@pytest.mark.parametrize(<br>        named_args[&#39;name&#39;], named_args[&#39;args&#39;], ids=named_args[&#39;ids&#39;])<br>def test_funcs_with_naming_func(a):<br>    assert a ** 3 == a * (a ** 2)</span></div></blockquote><div><br></div><br>- dpb<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Mar 22, 2015 at 10:53 PM, dpb dpb <span dir="ltr">&lt;<a href="mailto:dpb.mediamath@gmail.com" target="_blank">dpb.mediamath@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">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:<div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace">import pytest <br></font><font face="monospace, monospace"> <br></font><font face="monospace, monospace">@pytest.mark.parametrize(&#39;a&#39;, range(3)) <br></font><font face="monospace, monospace">def test_funcs_without_name(a): <br></font><font face="monospace, monospace">    assert a ** 3 == a * (a ** 2)</font> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace"><br></font><font face="monospace, monospace">@pytest.mark.parametrize(&#39;a&#39;, range(3), ids=[&#39;value of a&#39;] * 3) <br></font><font face="monospace, monospace">def test_funcs_with_name(a): <br></font><font face="monospace, monospace">    assert a ** 3 == a * (a ** 2)<br></font><font face="monospace, monospace">    <br></font><font face="monospace, monospace">$ py.test params/demo_ids.py -v<br></font><font face="monospace, monospace">============================= test session starts ==============================<br></font><font face="monospace, monospace">platform darwin -- Python 3.4.1 -- py-1.4.25 -- pytest-2.6.3 -- ...<br></font><font face="monospace, monospace">collected 6 items </font><font face="monospace, monospace"><br></font><font face="monospace, monospace">params/demo_ids.py::test_funcs_without_name[0] PASSED<br></font><font face="monospace, monospace">params/demo_ids.py::test_funcs_without_name[1] PASSED<br></font><font face="monospace, monospace">params/demo_ids.py::test_funcs_without_name[2] PASSED<br></font><font face="monospace, monospace">params/demo_ids.py::test_funcs_with_name[value of a] PASSED<br></font><font face="monospace, monospace">params/demo_ids.py::test_funcs_with_name[value of a] PASSED<br></font><font face="monospace, monospace">params/demo_ids.py::test_funcs_with_name[value of a] PASSED</font><font face="monospace, monospace"><br></font><font face="monospace, monospace">=========================== 6 passed in 0.02 seconds ===========================<br></font><font face="monospace, monospace">$ </font></blockquote></div><div><br></div><div>If I want to show both the value of each argument and its name, I can do something like this:</div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace">def fn(n): <br></font><font face="monospace, monospace">    return range(n)<br></font><font face="monospace, monospace"> <br></font><font face="monospace, monospace">@pytest.mark.parametrize(&#39;a&#39;, fn(3), ids=[&#39;a: {}&#39;.format(i) for i in fn(3)])<br></font><font face="monospace, monospace">def test_funcs_with_naming_func(a):<br></font><font face="monospace, monospace">    assert a ** 3 == a * (a ** 2) </font> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace"><br></font><font face="monospace, monospace">params/demo_ids.py::test_funcs_with_naming_func[a: 0] PASSED<br></font><font face="monospace, monospace">params/demo_ids.py::test_funcs_with_naming_func[a: 1] PASSED<br></font><font face="monospace, monospace">params/demo_ids.py::test_funcs_with_naming_func[a: 2] PASSED</font></blockquote></div><div><br></div><div>I can see to it that neither the argnames nor the argvalues appear more than once in the set of parameters to <span style="font-family:monospace,monospace">pytest.markparametrize</span>.</div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace">def params(n):</font></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace">    fn = </font><span style="font-family:monospace,monospace">range(n)</span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace">    return {<span>{</span>&#39;name&#39;<span>: </span>&#39;a&#39;<span>, </span>&#39;args&#39;: </font><span style="font-family:monospace,monospace">fn</span><font face="monospace, monospace">, &#39;ids&#39;: [&#39;a: {}&#39;.format(i) for i in fn]}</font></blockquote>







<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace"> <br></font><span style="font-family:monospace,monospace">named_args</span><font face="monospace, monospace"> = </font><span style="font-family:monospace,monospace">params</span><span style="font-family:monospace,monospace">(</span><span style="font-family:monospace,monospace">3)</span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace"> <br></font><font face="monospace, monospace">@pytest.mark.parametrize(<span>named_args</span><span>[</span><span>&#39;name&#39;</span><span>]</span>, </font><span style="font-family:monospace,monospace">pairs</span><font face="monospace, monospace">[&#39;args&#39;], ids=</font><span style="font-family:monospace,monospace">pairs</span><font face="monospace, monospace">[&#39;ids&#39;]) <br></font><font face="monospace, monospace">def test_funcs_with_naming_func(a): <br></font><font face="monospace, monospace">    assert a ** 3 == a * (a ** 2) </font></blockquote></div><div><br></div><div>But I wonder if there is a more compact way to accomplish this within Pytest itself, without the separate function</div><div><br></div><div>Thanks for your time.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>- dpb </div><div><br></div><div><br></div></font></span></div>
</blockquote></div><br></div>