[TIP] dynamically retrieving fixtures which depend on generated parameters from the comand-line
Elizabeth Lin
elin at splunk.com
Thu May 16 12:05:07 PDT 2013
Hi Holger,
Thanks again for the help. I was looking at it some more and ended up
having "fixA" not rely on the generatedfix fixture, and instead just
re-using that code as a regular function. Seems a little cleaner since it
avoids having to use skips or removal of unneeded params. Mods here in
case you're interested:
https://gist.github.com/elizabethlin/7754f0324b8f7b1c7388
the collection vs setup time info is useful. I remember there used to be
a text file included with the py package that included architecture
information, which was also very useful. Is this kind of info still
available somewhere?
Thanks,
Liz
On 5/15/13 12:54 AM, "holger krekel" <holger at merlinux.eu> wrote:
>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
>itshThanks
>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
>
More information about the testing-in-python
mailing list