Skip to main content

Testcompose

Testcompose

Testcompose - A clean and better way to test your Python containerized applications.

PyPI - Python Version PyPI - Implementation PyPI PyPI - Downloads Tests


Testcompose provides an easy way of using docker containers for functional and integration testing. It allows for combination of more than one containers and allows for interactions with these containers from your test code without having to write extra scripts for such interactions. I.e providing a docker compose kind of functionality with the extra benefit of being able to fully control the containers from test codes.

This is inspired by the testcontainers-python project and goes further to add a few additional functionalities to improve software integration testing while allowing the engineer to control every aspect of the test.


Install testcompose using pip:

$ pip install testcompose

testcompose requires Python 3.7+.

Using a config file. See the Quickstart for other options

# Testcompose include a cli utility to help create a quickstart config file that should be update to suit the needs of the user.
# Create a config file by running the command

testcompose generate-template --help   # shows the help and exit.

# To generate template config for an app and a db combination, run the command

testcompose generate-template --component db --component app

# The above command ouputs to stdout. To output to a file, include a filepath as below

testcompose generate-template --component db --component app --template-file some-valid-file-location.yaml

A sample of the config file is represented below:

services:
  - name: database
    image: "postgres:13"
    command: ""
    environment:
      POSTGRES_USER: postgres
      POSTGRES_DB: postgres
      POSTGRES_PASSWORD: password
    exposed_ports:
      - 5432
    log_wait_parameters:
      log_line_regex: "database system is ready to accept connections"
      wait_timeout_ms: 30000
      poll_interval_ms: 2000
  - name: application
    image: "python:3.9"
    command: "/bin/bash -x /run_app.sh"
    environment:
      DB_URL: "${database.postgres_user}:${database.postgres_password}@${database.container_hostname}:5432/${database.postgres_db}"
    volumes:
      - host: "docker-test-files/run_app.sh"
        container: "/run_app.sh"
        mode: "ro"
        source: "filesystem"
      - host: "docker-test-files/app.py"
        container: "/app.py"
        mode: "ro"
        source: "filesystem"
    exposed_ports:
      - "8000"
    log_wait_parameters:
      log_line_regex: ".*Application startup complete.*"
      wait_timeout_ms: 45000
      poll_interval_ms: 2000
    http_wait_parameters:
      http_port: 8000
      response_status_code: 200
      end_point: "/ping"
      startup_delay_time_ms: 30000
      use_https": false
    depends_on:
      - database

Verify it as follows:

import json
from typing import Any, Dict
from requests import Response, get
from testcompose.configs.service_config import Config
from testcompose.models.bootstrap.container_service import ContainerServices
from testcompose.models.container.running_container import RunningContainer
from testcompose.run_containers import RunContainers

config_services: ContainerServices = TestConfigParser.parse_config(file_name='some-config.yaml')
running_config: Config = Config(test_services=config_services)

with RunContainers(
        config_services=config_services,
        ranked_services=running_config.ranked_config_services,
) as runner:
    assert runner
    app_container_srv_name = "application"
    app_service: RunningContainer = runner.running_containers[app_container_srv_name]
    app_env_vars: Dict[str, Any] = app_service.config_environment_variables
    mapped_port = app_service.generic_container.get_exposed_port("8000")
    print(app_env_vars)
    app_host = app_service.generic_container.get_container_host_ip()
    assert app_env_vars
    assert mapped_port
    assert app_host
    response: Response = get(url=f"http://{app_host}:{int(mapped_port)}/version")
    assert response
    assert response.status_code == 200
    assert response.text
    assert isinstance(json.loads(response.text), dict)

Documentation

Quickstart

Special-Variables

Full-Doc

Release files for testcompose 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for testcompose 0.2.1
File Size Uploaded
testcompose-0.2.1.tar.gz 22.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for testcompose 0.2.1
File Interpreter ABI Platform
testcompose-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 53.5 kB

Release files / testcompose-0.2.1.tar.gz

Download URL testcompose-0.2.1.tar.gz
Size 22.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d1ab686bce8b3f2e76f24c7e8d0fcd25fc77ae870fc16a470feeb03659f004d2
BLAKE2b-256 checksum
How to use checksums
b893ac24322bd7a1f9018e84c321c733ff127d3b5458818329fb397e73c2dce0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / testcompose-0.2.1-py3-none-any.whl

Download URL testcompose-0.2.1-py3-none-any.whl
Size 31.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9f1973c574bb3d2c308b212c3a47a1a311f45a4a4aa70cb5e38227967beffd36
BLAKE2b-256 checksum
How to use checksums
76adf21ca6d31246e48c33a9f421577d61ec193ee27fddb8306df3300457163c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.5

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.4

3 release files

0.1.3

3 release files

0.1.2

3 release files

0.1.1

3 release files

0.0.12

3 release files

0.0.9

3 release files

0.0.8

3 release files

0.0.7

3 release files

0.0.6

3 release files

0.0.5

3 release files

0.0.4

3 release files

0.0.3

3 release files

0.0.2

3 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page