Skip to content Skip to sidebar Skip to footer

What Is A Correct Approach To Manage Test Data Using Pytest?

I need to create automated tests for several related apps and faced one problem with test data management between tests. The problem is that same data must be shared between severa

Solution 1:

I use conftest.py for test data. Fixtures are a recommended way to provide test data to tests. conftest.py is the recommended way to share fixtures among multiple test files.

So as for #2. I think it's fine to use conftest.py for test data.

Now for #1, "conftest.py becoming too big".

Especially for the top level conftest.py file, at test/conftest.py, you can move that content into one or more pytest plugin. Since conftest.py files can be thought of as "local plugins", the process for transforming them into plugins is not too difficult.

See https://docs.pytest.org/en/latest/writing_plugins.html

Solution 2:

You might be interested in looking at pytest-cases: it was actually designed to address this question. You will find plenty of examples in the doc, and cases can sit in dedicated modules, in classes, or in the test files - it really depends on your needs. For example putting two kind of test data generators in the same module:

from pytest_cases import parametrize_with_cases, parametrize

defdata_a():
    return'a'@parametrize("hello", [True, False])defdata_b(hello):
    return"hello"if hello else"world"defuser_bob():
    return"bob"@parametrize_with_cases("data", cases='.', prefix="data_")@parametrize_with_cases("user", cases='.', prefix="user_")deftest_with_data(data, user):
    assert data in ('a', "hello", "world")
    assert user == 'bob'

See documentation for details. I'm the author by the way ;)

Post a Comment for "What Is A Correct Approach To Manage Test Data Using Pytest?"