问题
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 several apps and/or different APIs. Now I have next structure with pytest that working good for me but I doubt that use test data managment in conftest.py is correct approach:
Overall structure looks like:
tests/
conftest.py
app1/
conftest.py
test_1.py
test_2.py
app2/
conftest.py
test_1.py
test_2.py
test_data/
test_data_shared.py
test_data_app1.py
test_data_app2.py
Here is example of test data in tests/conftest.py:
from test_data.test_data_shared import test_data_generator, default_data
@pytest.fixture
def random_email():
email = test_data_generator.generate_random_email()
yield email
delete_user_by_email(email)
@pytest.fixture()
def sign_up_api_test_data(environment, random_email):
"""
environment is also fixture, capture value from pytest options
"""
data = {"email": random_email, "other_data": default_data.get_required_data(), "hash": test_data_generator.generate_hash(environment)}
yield data
do_some_things_with_data(data)
Its very comfort to use fixture for that purposes, because postconditions, scopes and other sweet things (note that apps have a lot of logic and relationship, so I cannot simply hardcode data or migrate it in json file for example) Similar things can be found in tests/app1/conftest.py and tests/app2/conftest.py for data that used in app1 and app 2 accordingly.
So, here is 2 problems: 1. conftest.py become a monster with a lot of code 2. as I know, using conftest for test data is a bad approach or I'm wrong?
Thanks in advance!
回答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
回答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
def data_a():
return 'a'
@parametrize("hello", [True, False])
def data_b(hello):
return "hello" if hello else "world"
def user_bob():
return "bob"
@parametrize_with_cases("data", cases='.', prefix="data_")
@parametrize_with_cases("user", cases='.', prefix="user_")
def test_with_data(data, user):
assert data in ('a', "hello", "world")
assert user == 'bob'
See documentation for details. I'm the author by the way ;)
来源:https://stackoverflow.com/questions/54330834/what-is-a-correct-approach-to-manage-test-data-using-pytest