Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use pytest fixture outside test run?

I have a set of fixtures to setup fairly big data set. The result of the setup is stored in a database (scope=function) for a report rendering regression test. The fixtures are not parametrized, so there is only one setup that comes out of it, but the regression test has a few of those interdependent fixtures as parameters to access the objects without additional queries. pytest uses in-memory database and rolls-back after each test, so after finishing the tests, the setup is not available.

I would like to use that database setup outside pytest for demo and front-end automated tests.

How to get the result of the pytest fixture in development database?

An example without details, to show the structure of the fixture:

@pytest.fixture
def customer():
    return mommy.make(Customer, name='Customer LTD')

@pytest.fixture(autouse=True)
def inventory_types(customer):
    return seeder.seed_inventory_types('A,B,C', customer=customer)


@pytest.fixture(autouse=True)
def inventory(
    good,
    bad,
    ugly,
    common,
    ...
):
    return

@pytest.fixture
def good(customer, patterns):
    vehicle = mommy.make(
        Inventory, 
        name='Good', 
        type=inventory_types.A, 
        customer=customer
    )

@pytest.fixture
def bad(customer, patterns):
    return make(
        Inventory, 
        name='Bad', 
        type=inventory_types.A,  
        customer=customer
    )

@pytest.fixture
def ugly(customer, patterns):
    return mommy.make(
        Inventory, 
        name='Ugly', 
        type=inventory_types.B, 
        customer=customer
    )

@pytest.fixture
def common(customer, patterns):
    return mommy.make(
        Inventory, 
        name='Common', 
        type=inventory_types.C, 
        customer=customer
    )


def test(good, customer):
    assert good in customer.inventory
like image 294
dhill Avatar asked Jul 17 '19 09:07

dhill


People also ask

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 a pytest fixture be a test?

Pytest fixtures are functions that can be used to manage our apps states and dependencies. Most importantly, they can provide data for testing and a wide range of value types when explicitly called by our testing software. You can use the mock data that fixtures create across multiple tests.

Can pytest fixtures use other fixtures?

A fixture can use multiple other fixtures. Just like a test method can take multiple fixtures as arguments, a fixture can take multiple other fixtures as arguments and use them to create the fixture value that it returns.

How does fixture work in pytest?

Fixtures define the steps and data that constitute the arrange phase of a test (see Anatomy of a test). In pytest, they are functions you define that serve this purpose. They can also be used to define a test's act phase; this is a powerful technique for designing more complex tests.


1 Answers

It seems that you can call the function wrapped by the fixture decorator like this: customer.__pytest_wrapped__.obj(). Obviously that's not a public API and so might change.

It looks like an alternative would be to structure your fixture code differently, so that it also exposes a normal function. See the name argument mentioned here: https://docs.pytest.org/en/stable/deprecations.html#calling-fixtures-directly

like image 161
5fec Avatar answered Oct 13 '22 12:10

5fec