-
Notifications
You must be signed in to change notification settings - Fork 14
Integration Testing
- 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.
To run the tests locally on a Windows machine, you may need to complete the following steps:
-
Make sure Docker is running.
-
Install Windows Subsystem for Linux and Ubuntu.
-
Open WSL(Ubuntu) terminal and configure your username and password with the following commands:
sudo apt update
sudo apt full-upgrade
-
On Docker desktop, navigate to the
Resources
tab then WSL integration and ensure that WSL and Ubuntu integrations are checked/on. -
Add your username to the docker group. In a WSL(Ubuntu) terminal, run the following command:
sudo usermod -a -G docker <your username that you created in step 3>
If the docker group doesn't exist, create first it with
sudo groupadd docker
-
Create a virtual environment for the container and activate with the following commands:
python3 -m venv .venv
source .venv/bin/activate
You are now able to run the integration tests locally! Note: you may need to install pytest with pip3 install pytest
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.