[TIP] How to use same pytest hook at module and function scopes?

arun kali raja arunsep886 at gmail.com
Wed Feb 8 10:56:16 PST 2017


Hi Bruno,

yeah this will solve the case of calling the fixture(which doesnt do
function level configuration but rather it takes the backup of certain
files in the system before the TC executes and restores them to original
state after the TC is complete) before each TC execution .

But i need the same to be called before the common_configuration part as
well like below:

configurationBackup
common_Configuration
configurationBackup
TC_1
configurationBackup.finalizer
configurationBackup
TC_2
configurationBackup.finalizer
configurationBackup
TC_3
configurationBackup.finalizer
configurationBackup.finalizer(which is done before common_Configuration)


My hurdle is to handle both these cases thorugh single fixture itself..

As for the fixture for a hook i think i misunderstud modularity funda,
where a fixture can use another fixture and tried to apply the same to a
hook.. my bad!!

Regards
Arun kaliraja.B


On 8 February 2017 at 20:12, Bruno Oliveira <nicoddemus at gmail.com> wrote:

> Hi Arun,
>
> On Wed, Feb 8, 2017 at 12:22 PM arun kali raja <arunsep886 at gmail.com>
> wrote:
>
>>
>>
>> I tried couple of things.
>>
>>    1.
>>
>>    declaring configurationBackup in conftest.py as a function level
>>    fixture)as an autouse fixture). But the problem here is its executed before
>>    common_configuration fixture for TC_1
>>
>>
> One way to enforce this order is to make the function-level fixture depend
> on the module-level fixture (common_configuration?):
>
>     @pytest.fixture(scope='module')
>     def common_configuration():
>         ...
>
>     @pytest.fixture(autouse=True)
>     def function_level_configuration(common_configuration):
>         ...
>
>
>
>>    1.
>>
>>    tried using standar pytest hooks. There is a pytest hook
>>    pytest_runtest_call.. i was thinking if i can decorate this hook function
>>    with my configurationBackup fixture like this:
>>
>>       @pytest.mark.usefixtures("configurationBackup")
>>       def pytest_runtest_call(item):
>>
>>
>> But this also doesnt seem to work... my configurationBackup fixture is
>> not called at all..
>>
>
> This is unfortunately a common source of confusion, but
> "@pytest.mark.usefixtures" only works for test items, not fixtures and
> hooks. But I believe there's a note about this in the docs.
>
> My previous suggestion should work, this kind of dependency between
> fixtures is common and well supported. If it doesn't, it means I probably
> didn't understand your problem completely, in which case I would ask to
> post a more detailed pseudo-code which better conveys the problem.
>
> Hope this helps,
> Bruno.
>



-- 
அன்புடன்,
பா. அருண் காளி ராசா
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20170209/895c5158/attachment-0001.htm>


More information about the testing-in-python mailing list