Skip to content

Integration Testing

Marcelle edited this page Aug 19, 2024 · 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 locally on Windows

To run the tests locally on a Windows machine, you may need to complete the following steps:

  1. Make sure Docker is running.

  2. Install Windows Subsystem for Linux and Ubuntu.

  3. Open WSL(Ubuntu) terminal and configure your username and password with the following commands: sudo apt update sudo apt full-upgrade

  4. On Docker desktop, navigate to the Resources tab then WSL integration and ensure that WSL and Ubuntu integrations are checked/on.

  5. 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

  6. 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

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.