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

arun kali raja arunsep886 at gmail.com
Wed Feb 8 11:36:23 PST 2017


Thanks Bruno..

I was trying to find out if I can solve it by having a single fixture
itself.. seems like its not possible with a single fixture..

My usecase is a little more complicated..

My test case structure will be

BaseFolder/scenarioA/test.py
BaseFolder/scenarioB/test.py
.
.

BaseFolder/scenarioN/test.py

Each of the test.py has its own common_configuration function definition
and TCs..(test.py won't have any class def)
😊

My Conftest.py will be in BaseFolder/

So if these two fixtures would go into the conftest.py can they still call
the common_configuration function.?

Regards
Arun Kaliraja

On 09-Feb-2017 12:50 AM, "Bruno Oliveira" <nicoddemus at gmail.com> wrote:

Hi Arun,

On Wed, Feb 8, 2017 at 4:56 PM arun kali raja <arunsep886 at gmail.com> wrote:

> 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)
>
>
>
>
If I understand your example correctly, you can use two fixtures then:

@pytest.fixture(scope='session')
def module_config():
    configurationBackup
    common_Configuration
    yield
    configurationBackup.finalizer


@pytest.fixture(autouse=True)
def function_config(module_config):
    configurationBackup
    yield
    configurationBackup.finalizer

module_config's "configurationBackup.finalizer" will be called last for the
module, after TC_3's "configurationBackup.finalizer".

Cheers,
Bruno.

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20170209/7b947232/attachment.htm>


More information about the testing-in-python mailing list