-
Notifications
You must be signed in to change notification settings - Fork 15
Integration Testing
emmastephenson edited this page Sep 14, 2023
·
7 revisions
- Import
testcontainers[compose]
into the requirements.txt file for the container - Create a new
integration/
folder inside thetests/
folder for the container (this setup is needed for CI) - Create a
docker-compose.yaml
file inside ofintegration/
that just starts the container and exposes it on8080
- Create
conftest.py
in the integration folder and add setup/teardown methods. These should start the container using thedocker-compose
script you just wrote on startup, and stop the container after all tests have run. - Create a new file in
integration/
to hold the actual tests. The tests themselves should hit available endpoints on the container and verify that the responses work as expected. -
IMPORTANT: You need to mark the tests as integration tests with
@pytest.mark.integration
. Otherwise they'll run as unit tests on CI (no good because they're slow!)
To run the tests locally, you'll need Docker running. The first test execution will take some time as the container needs to build from scratch, but subsequent test runs will be faster as the container and imports are cached.
Goal | Command |
---|---|
Run all tests | pytest |
Run only integration tests | pytest -m "integration" |
Run only unit tests | pytest -m "not integration" |
"integration" is a pytest marker.
The integration tests only run in CI if there's a folder following the pattern /containers/{container-to-test}/tests/integration
. This is because if you just run `pytest -m "integration" at the root directory of each container, any containers without marked integration tests will fail.
If unit tests fail, integration tests should automatically be cancelled to ensure we're conserving Action resources.