Database Fixtures
Understand how to create and manage database fixtures for PostgreSQL to support integration tests in Python. Explore loading configurations, session management, and clean test setups using pytest and Docker. Learn to keep tests isolated and reliable by resetting database state between runs.
When we define the database configuration in a JSON file, we need a fixture that loads that same configuration. This is so we can connect to the database during our tests. Since we already have the function read_json_configuration in our management script, all we need to do is wrap it.
Update the conftest file
Update the common conftest file
This is a fixture that isn’t specific to the PostgreSQL repository, so we’ll introduce it in the following code.
As we can see in the code above, we hardcode the name of the configuration file for the sake of simplicity. Alternately, we can also create an environment variable with the application configuration in the management script and read it from there.