mercredi 23 janvier 2019

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 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!

Aucun commentaire:

Enregistrer un commentaire