[TIP] dynamically retrieving fixtures which depend on generated parameters from the comand-line

holger krekel holger at merlinux.eu
Wed May 15 00:54:56 PDT 2013


Hi Elizabeth,

On Tue, May 14, 2013 at 22:14 +0000, Elizabeth Lin wrote:
> Here's the simplified example code:
> https://gist.github.com/elizabethlin/7754f0324b8f7b1c7388
> 
> this gets generates the following tests:
> test_example.py:31: TestExample.test_one[std] FAILED
> test_example.py:35: TestExample.test_two[std] FAILED
> test_example.py:39: TestExample.test_three[std] FAILED
> test_example.py:31: TestExample.test_one[virt] FAILED
> test_example.py:35: TestExample.test_two[virt] FAILED
> 
> 
>  when really I want
> test_example.py:31: TestExample.test_one[std] FAILED
> test_example.py:35: TestExample.test_two[std] FAILED
> test_example.py:39: TestExample.test_three[std] FAILED
> test_example.py:31: TestExample.test_one[virt-typeA] FAILED
> test_example.py:35: TestExample.test_two[virt-typeA] FAILED
> " [virt-typeB]
> " [virt-typeB]
> 
> Can someone help me out?  I imagine that pytest_generate_tests gets called at an earlier step, before the fixtures are executed, and at that time, it can't detect that we need the parametrization for generated fix to happen.  There's probably other reasons this won't work, but I'm having trouble decifering the pytest code, and the flow of it.

you may or may not be aware of this:

- test parametrization happens at collection time
- Fixture functions execute at setup time

If you use "request.getfuncargvalue()" during test setup pytest has 
no chance of knowing about this usage during collection.  If "virt" is a heavy
resource and you want to avoid creating it, it's probably best to still
parametrize it into existence during collection but then shortcut its
creation (i.e. pytest.skip() it) in its fixture function.  

You could remove not needed parameter sets in a pytest_collect_modifyitems 
hook (in a conftest.py) to avoid the skipping.  See a (somewhat hacky) way
to achieve that attached.   I wouldn't be very happy with using that myself,
however, and rather try to aim for a simpler way even if it means writing
more verbose tests or more fixture names into test signatures. 

HTH,

holger

-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_example.py
Type: text/x-python
Size: 629 bytes
Desc: not available
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20130515/b3902884/attachment.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pytest_myplugin.py
Type: text/x-python
Size: 762 bytes
Desc: not available
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20130515/b3902884/attachment-0001.py>


More information about the testing-in-python mailing list