Skip to main content

Collection of Testcontainers, pytest fixtures and test clients for end-to-end/integration testing for Python Tomodachi framework. A great starting point to learn more about Testcontainers and necessity of integration testing.

Project description

Tomodachi Testcontainers

Build Status FOSSA Status Quality Gate Status CodeScene Code Health CodeScene System Mastery codecov Maintainability

Tomodachi Testcontainers is a Python library built on top of testcontainers-python. It provides Testcontainers, pytest fixtures, and test clients for convenient use of Testcontainers with pytest and testing applications built with the Python Tomodachi framework.

This library was created to explore and learn Testcontainers. Although initially intended to be used with the Tomodachi framework, it works for testing applications built with any other Python framework like Flask, FastAPI, Django, etc.

What is Testcontainers?

Testcontainers is an open-source framework for providing throwaway, lightweight instances of databases, message brokers, web browsers, or just about anything that can run in a Docker container. It facilitates the use of Docker containers for functional, integration, and end-to-end testing. — https://testcontainers.com/

To learn more about what Testcontainers are and what problems they solve, take a look at the Getting Started guide in the official Testcontainers documentation - https://testcontainers.com/getting-started/

Documentation

Find documentation at https://filipsnastins.github.io/tomodachi-testcontainers/

The official Testcontainers documentation is at https://testcontainers.com/

Installation

Install with pip:

pip install tomodachi-testcontainers

Install with Poetry:

poetry add --group dev tomodachi-testcontainers

Find a list of extras in the installation reference.

A Simple Example

The hello, world Tomodachi service:

# src/hello.py
import tomodachi
from aiohttp import web


class Service(tomodachi.Service):
    @tomodachi.http("GET", r"/hello/?")
    async def hello(self, request: web.Request) -> web.Response:
        name = request.query.get("name", "world")
        return web.json_response({"message": f"Hello, {name}!"})
  • testcontainer_image fixture builds a Docker image with a Dockerfile from the current working directory.
  • tomodachi_container fixture starts a new Docker container running the hello service on a randomly available port.
  • test_hello_testcontainers sends a GET /hello?name=Testcontainers request to the running container and asserts the response.
# tests/test_hello.py
from typing import Generator, cast

import httpx
import pytest

from tomodachi_testcontainers import TomodachiContainer


@pytest.fixture(scope="session")
def tomodachi_container(testcontainer_image: str) -> Generator[TomodachiContainer, None, None]:
    with TomodachiContainer(testcontainer_image).with_command(
        "tomodachi run readme/hello.py --production"
    ) as container:
        yield cast(TomodachiContainer, container)


@pytest.mark.asyncio()
async def test_hello_testcontainers(tomodachi_container: TomodachiContainer) -> None:
    async with httpx.AsyncClient(base_url=tomodachi_container.get_external_url()) as client:
        response = await client.get("/hello", params={"name": "Testcontainers"})

    assert response.status_code == 200
    assert response.json() == {"message": "Hello, Testcontainers!"}

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tomodachi_testcontainers-1.1.0.tar.gz (21.9 kB view details)

Uploaded Source

Built Distribution

tomodachi_testcontainers-1.1.0-py3-none-any.whl (32.3 kB view details)

Uploaded Python 3

File details

Details for the file tomodachi_testcontainers-1.1.0.tar.gz.

File metadata

File hashes

Hashes for tomodachi_testcontainers-1.1.0.tar.gz
Algorithm Hash digest
SHA256 6c9259ad9dd2dad8a39cf0e8729aa4c07b0d3ee1ac3408091cc961ab5543407c
MD5 7e6852bacca851ce633c5122f2cda6cd
BLAKE2b-256 3a72846517299086cb4d4415abe1d15d534f34194984dbabd57bcacd87d85867

See more details on using hashes here.

File details

Details for the file tomodachi_testcontainers-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for tomodachi_testcontainers-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7c28174614828d3b1253613fcc68ce8f762c991bfe0ab4a4703cbf41bd2616e5
MD5 37bf0c9fbd28be51d2c1da259adcd78f
BLAKE2b-256 b1245f260170a98be27ae3ae439044207d088e2b92fac5baa78e68c95d49af4c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page