Skip to content

Integration Testing

emmastephenson edited this page Sep 14, 2023 · 7 revisions

Writing a new integration test

  1. Import testcontainers[compose] into the requirements.txt file for the container
  2. Create a new integration/ folder inside the tests/ folder for the container (this setup is needed for CI)
  3. Create a docker-compose.yaml file inside of integration/ that just starts the container and exposes it on 8080
  4. Create conftest.py in the integration folder and add setup/teardown methods. These should start the container using the docker-compose script you just wrote on startup, and stop the container after all tests have run.
  5. 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.
  6. 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!)

Running integration tests locally

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.

Running integration tests in CI

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.

Clone this wiki locally