pytest-django access to db when using pytest_generate_tests

16 Views Asked by At

Is there a way to be able to indirectly generate parameters via a database query when using pytest-django. For example if I have the following:

Some code from here.

pytestmark = pytest.mark.django_db


def pytest_generate_tests(metafunc):
    if "foo" in metafunc.fixturenames:
        metafunc.parametrize("db", Foo.objects.filter(bar=1), indirect=True)


def test_foo(load_all_foo, foo):  
    # do things with all foo and test

I am not sure what the deferred parametrization means here. Looking at code, pytest as well as pytest-django was not quite helpful.

In the link above the example is still using an initial static list of params which then get converted to an actual db object.

Where it would be useful to me is when I have a test that has a large setup and teardown time, plus the list of Foos can change and ocassionally fail. However rather than stopping and exiting when a test with one specific Foo fails I would like to continue and see the failed one in the report.

I could sort of call metafunc.parametrize with an array of Nones and then yield a Foo in another fixture but that's not really a test.

Any help would be greatly appreciated.

0

There are 0 best solutions below