All Projects → lovelysystems → lovely-pytest-docker

lovelysystems / lovely-pytest-docker

Licence: Apache-2.0 license
Pytest plugin providing the ability to use docker-compose services as fixtures.

Programming Languages

python
139335 projects - #7 most used programming language
kotlin
9241 projects

Projects that are alternatives of or similar to lovely-pytest-docker

Pytest Mimesis
Mimesis integration with the pytest test runner. This plugin provider useful fixtures based on providers from Mimesis.
Stars: ✭ 46 (-36.99%)
Mutual labels:  fixtures, pytest
Pytest Deadfixtures
Plugin to list unused fixtures in your tests
Stars: ✭ 89 (+21.92%)
Mutual labels:  fixtures, pytest
pytest-datafixtures
Data fixtures for pytest made simple
Stars: ✭ 24 (-67.12%)
Mutual labels:  fixtures, pytest
yii2-faker
Yii 2 Faker extension
Stars: ✭ 99 (+35.62%)
Mutual labels:  fixtures
gtbot
구글 번역 API를 이용한 슬랙 번역 봇입니다.
Stars: ✭ 34 (-53.42%)
Mutual labels:  pytest
pytest-spark
pytest plugin to run the tests with support of pyspark
Stars: ✭ 65 (-10.96%)
Mutual labels:  pytest
pytest-notebook
A pytest plugin for regression testing and regenerating Jupyter Notebooks
Stars: ✭ 35 (-52.05%)
Mutual labels:  pytest
pytest-pipeline
Pytest plugin for functional testing of data analysis pipelines
Stars: ✭ 19 (-73.97%)
Mutual labels:  pytest
pytest-eth
PyTest plugin for testing smart contracts for Ethereum blockchain.
Stars: ✭ 23 (-68.49%)
Mutual labels:  pytest
prodigyqa
Unified Test Automation Framework in Python
Stars: ✭ 35 (-52.05%)
Mutual labels:  pytest
pytest-snail
Plugin for adding a marker to slow running tests. 🐌
Stars: ✭ 15 (-79.45%)
Mutual labels:  pytest
jest-fixtures
Use file system fixtures in Jest
Stars: ✭ 39 (-46.58%)
Mutual labels:  fixtures
python-ood
💠 Essential object oriented design (python, pytest, travisCI)
Stars: ✭ 38 (-47.95%)
Mutual labels:  pytest
fixturez
Easily create and maintain test fixtures in the file system
Stars: ✭ 57 (-21.92%)
Mutual labels:  fixtures
python-page-object
📔 Page object design pattern implementation (python, pom, selenium, pytest, travisCI)
Stars: ✭ 41 (-43.84%)
Mutual labels:  pytest
pytest-serverless
Automatically mocks resources from serverless.yml in pytest using moto.
Stars: ✭ 26 (-64.38%)
Mutual labels:  pytest
pytest-watcher
Rerun pytest when your code changes
Stars: ✭ 60 (-17.81%)
Mutual labels:  pytest
pytest-github-actions-annotate-failures
Pytest plugin to annotate failed tests with a workflow command for GitHub Actions
Stars: ✭ 58 (-20.55%)
Mutual labels:  pytest
cloudrun-fastapi
FastAPI on Google Cloud Run
Stars: ✭ 112 (+53.42%)
Mutual labels:  pytest
test-tools
Improves PHPUnit testing productivity by adding a service container and self-initializing fakes
Stars: ✭ 25 (-65.75%)
Mutual labels:  fixtures

Lovely Pytest Docker

https://travis-ci.com/lovelysystems/lovely-pytest-docker.svg?branch=master

Create simple Pytest fixtures for writing integration tests based on Docker containers. The framework provides a service class to start and stop containers based Docker Compose. Each single container can be started individually.

Some parts of this package are taken from https://github.com/AndreLouisCaron/pytest-docker

Usage with Pytest

The docker compose file should contain all desired containers and the ports should be exposed. In the following example we want to start the app to test and a SQL database (Crate). Let's assume there is a Dockerfile for the app in the same folder as the docker compose file:

version: "3"
services:
  app:
    build: .
    ports:
      - "8080"
    depends_on:
      - "crate"

  crate:
    image: crate:latest
    ports:
      - "4200"

In the conftest.py file we can declare the docker fixtures for each service we want to be able to start in the tests:

import pytest

@pytest.fixture(scope='session')
def docker_app(docker_services):
    docker_services.start('app')
    public_port = docker_services.wait_for_service("app", 8080)
    url = "http://{docker_services.docker_ip}:{public_port}".format(**locals())
    return url

@pytest.fixture(scope='session')
def docker_crate(docker_services):
    docker_services.start('crate')
    public_port = docker_services.wait_for_service("crate", 4200)
    dsn = "{docker_services.docker_ip}:{public_port}".format(**locals())
    return dsn

By default the fixture will look for the docker-compose.yml file in the tests subfolder of the path where pytest.ini resides (or the project's root directory if no ini file is given - as in the tests example). In many cases you will want to override the location for the docker compose files. Just overwrite the docker_compose_files fixture in your conftest.py file:

@pytest.fixture(scope='session')
def docker_compose_files(pytestconfig):
    """Get the docker-compose.yml absolute path.
    Override this fixture in your tests if you need a custom location.
    """
    return [
        project_path('docker', 'docker-compose.yml'),
    ]

In your test file declare the fixtures you want to use:

def test_something(docker_app, docker_crate):
    # e.g. initialize database
    ...
    # test something (e.g. request to docker_app)
    ...

A working configuration and test example can be found in the tests folder of this package.

Execution in Docker Container

It's possible to execute a command inside one of the Docker containers. Use the exec method of the docker_services fixture:

def test_execute(docker_services):
    # the first argument is the service name of the compose file,
    # the following arguments build the command to run
    res = docker_services.execute('crate', 'ls', '-a')

Stopping a Docker Container

It's possible to stop single Docker containers. Use the stop method of the docker_services fixture:

def test_stop(docker_services):
    # the first argument is the service name of the compose file,
    # the following arguments build the command to run
    res = docker_services.stop('crate')

Wait for Service

The wait_for_service method of the service module checks whether the docker service is really started. By default it makes a HTTP GET request to the server's / endpoint. The service will retry to check until a timeout of 30 seconds has passed.

Custom Service Checker

Some services may work differently and require a custom checker.

Create a custom service checker function which receives the IP address and the port as parameters:

def custom_service_checker(ip_address, port):
    # if service is ready
    return True
    # otherwise return False

In the fixture provide the custom service checker function as check_service parameter to the wait_for_service method:

@pytest.fixture(scope='session')
def docker_custom_service(docker_services):
    docker_services.start('custom_service')
    public_port = docker_services.wait_for_service(
        "app",
        8080,
        check_server=custom_service_checker
    )
    url = "http://{docker_services.docker_ip}:{public_port}".format(**locals())
    return url

To use another request path with the default checker the url_checker method can be used to create a check_url method for another path:

docker_services.wait_for_service(
    "app",
    8080,
    check_server=url_checker('/probe_status'),
)

Run Tests

Tests are held in the tests directory. Running tests is done via the pytest package with:

./gradlew pytest
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].