Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytest - Calling a fixture from another fixture

I have a fixture that is returning an object of certain type and I have another fixture defined in another file that basically uses the object to do other things. But I am not able to return the object from my first fixture.

file-1

def fixture_1(s, **kwargs):
    def hook(s, **kwargs):
        p_b = s.get()
        p = p_b.build()
        yield p
    return hook

file-2 conftest.py

@pytest.fixture(scope='module')
def fixture_1(s, **kwargs):
    def hook(s, **kwargs):
        #Default implementation is no-op.
        pass
    return hook

@pytest.fixture(scope='module')
def fixture_2(s,b_p):
    some_p = fixture_1(s)
    current_status = s.start(some_p)

    print(current_status)
    yield current_status

I want to basically retrieve object p returned in file-1 fixture_1 and use it in file-2 fixture_2 fixture.

like image 760
AYa Avatar asked May 31 '19 23:05

AYa


People also ask

Can a fixture call another fixture pytest?

Fixtures can also be requested more than once during the same test, and pytest won't execute them again for that test. This means we can request fixtures in multiple fixtures that are dependent on them (and even again in the test itself) without those fixtures being executed more than once.

How do you call pytest fixture directly?

To access the fixture function, the tests have to mention the fixture name as input parameter. Pytest while the test is getting executed, will see the fixture name as input parameter. It then executes the fixture function and the returned value is stored to the input parameter, which can be used by the test.

Can pytest fixtures have arguments?

You can pass arguments to fixtures with the params keyword argument in the fixture decorator, and you can also pass arguments to tests with the @pytest.

Can you import pytest fixtures?

Fixtures and their visibility are a bit odd in pytest. They don't require importing, but if you defined them in a test_*. py file, they'll only be available in that file. You can however put them in a (project- or subfolder-wide) conftest.py to use them in multiple files.


1 Answers

It seems that you are using pytest fixtures wrong (looking at your arguments names).

I'd recommend you to go through https://docs.pytest.org/en/latest/fixture.html

There are two solutions for your problem:

###
# file_1
def not_really_a_fixture(s, **kwargs): # just some hook generator
    def hook(s, **kwargs):
        p_b = s.get()
        p = p_b.build()
        yield p
    return hook


###
# conftest.py
from file_1 import not_really_a_fixture

@pytest.fixture(scope='module')
def fixture_2(s,b_p): # s and b_p should be names of fixtures that need to run before this
    some_p = not_really_a_fixture(s)
    current_status = s.start(some_p)

    print(current_status)
    yield current_status

And:

###
# file_1
@pytest.fixture(scope='module')
def fixture_1(s): # s is name of another fixture
    # there is no point in having **kwargs as arg in pytest fixture
    def hook(s, **kwargs):
        #Default implementation is no-op.
        pass
    return hook

###
# conftest.py
from file_1 import fixture_1

@pytest.fixture(scope='module')
def fixture_2(s,b_p,fixture_1): # s and b_p should be names of fixtures that need to run before this
    # in fixture_1 is value returned by fixture_1, that means your hook func
    current_status = s.start(fixture_1)

    print(current_status)
    yield current_status
like image 146
MacHala Avatar answered Sep 29 '22 02:09

MacHala