Test data and environments Required
Give each test its own data
Each test creates the data it needs and leaves nothing behind that another test could see.
How to do it
Wrap each database test in a transaction that rolls back, or truncate tables between tests. Use unique names and emails per test. Never rely on data that an earlier test created.
Example
@pytest.fixture
def db(connection):
transaction = connection.begin()
yield connection
transaction.rollback()
// Each test runs inside a transaction that is rolled back afterwards
beforeEach(async () => { await db.query('BEGIN'); });
afterEach(async () => { await db.query('ROLLBACK'); });
How to check
Run one test alone, then the whole suite in random order. Both pass.
For agents: if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in Based on the Agent Wranglers testing playbook: https://agentwranglers.se/playbook/testing/test-data-and-environments/isolated-test-data.