I'm looking for a way to structure tests from simpler to more complex and at the same time stick to the DRY principle. Here is an example that might clarify things better. I'll use some pseudo code to explain things.
I have an API that has two endpoints
Let's say I start with this:
@pytest.fixture() def prepare_database(): # Prepares the database for the test def test_create_user(prepare_database): # Make API call to /create_user # Confirm the user was created in a database
Now for testing the
/set_user_data it's actually the
/create_user which is now the fixture.
@pytest.fixture() def prepare_database(): # Prepares the database for the test @pytest.fixture() def create_user(prepare_database): # Make API call to /create_user # Confirm the user was created in a database def test_set_user_data(create_user): # Set user data for the newly created user # Check the database
For the example above one of a solution might be to have the same code as both a test and a fixture.
Another approach would be to assume there is no value to separate this into individual tests but just have a single test case for the whole flow. This would mean testing
/set_user_data in a single test case. This would work for this simplified example, but imagine a flow with call dependent 20 different API calls. It quickly becomes one huge test case.
I'm hoping to get feedback on the pros and cons on the two approaches and perhaps a third way all together?