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
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 thehello
service on a randomly available port.test_hello_testcontainers
sends aGET /hello?name=Testcontainers
request to the running container and asserts the response.
# tests/test_hello.py
from typing import Generator
import httpx
import pytest
from tomodachi_testcontainers import DockerContainer, TomodachiContainer
@pytest.fixture(scope="session")
def tomodachi_container(testcontainer_image: str) -> Generator[DockerContainer, None, None]:
with TomodachiContainer(testcontainer_image).with_command(
"tomodachi run readme/hello.py --production"
) as container:
yield 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
- Documentation: https://filipsnastins.github.io/tomodachi-testcontainers/
- Releases and Changelog: https://github.com/filipsnastins/tomodachi-testcontainers/releases
- PyPI: https://pypi.org/project/tomodachi-testcontainers/
- Source Code: https://github.com/filipsnastins/tomodachi-testcontainers
- Reference - Code API: https://filipsnastins.github.io/tomodachi-testcontainers/reference/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file tomodachi_testcontainers-1.2.1.tar.gz
.
File metadata
- Download URL: tomodachi_testcontainers-1.2.1.tar.gz
- Upload date:
- Size: 22.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6ac98ae90c3783a9a541634b1c7eb4c16ef427cefc20a391775e68c78888a11 |
|
MD5 | fd9ff86b020db0d6e68d85c67ee0f332 |
|
BLAKE2b-256 | 3df5eadb23ed6310af2ab2a3a66d753feffe8372ac56d6988c331ee25de0d8a8 |
File details
Details for the file tomodachi_testcontainers-1.2.1-py3-none-any.whl
.
File metadata
- Download URL: tomodachi_testcontainers-1.2.1-py3-none-any.whl
- Upload date:
- Size: 32.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ece66a2bb32564f797032e610cfcd35f8a905030cfe15f450e97f93e183a15d0 |
|
MD5 | e6dd57fec2b175f8b7caff72587cc58e |
|
BLAKE2b-256 | 8a3c63a0dbf87ff58405a2f515ab4ac3318bfc0625802541fa3fa078ad736a3b |