跳转到主要内容

基于Docker容器的Pytest测试实用工具。

项目描述

可爱的Pytest Docker

PyPI PyPI Build Status

创建基于Docker容器的简单Pytest fixtures,用于编写集成测试。该框架提供了一个服务类,可以根据Docker Compose启动和停止容器。每个容器都可以单独启动。

本包的一些部分来自https://github.com/AndreLouisCaron/pytest-docker

与Pytest一起使用

docker compose文件应包含所有所需的容器,并且端口应该被暴露。在下面的示例中,我们想要启动要测试的应用程序和SQL数据库(Crate)。假设应用程序的Dockerfile与docker compose文件在同一文件夹中。

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

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

conftest.py文件中,我们可以声明每个我们想要在测试中启动的服务器的docker fixtures。

    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

默认情况下,固定装置将在pytest.ini文件所在路径的tests子目录中查找docker-compose.yml文件(如果没有提供ini文件,则默认为项目根目录,例如在测试示例中)。在许多情况下,您可能需要覆盖Docker Compose文件的位置。只需在您的conftest.py文件中覆盖docker_compose_files固定装置即可。

    @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'),
        ]

在您的测试文件中声明您想要使用的固定装置。

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

在本软件包的tests文件夹中可以找到工作配置和测试示例。

Docker容器内执行

可以在Docker容器中执行命令。使用docker_services固定装置的exec方法:

    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')

停止Docker容器

可以停止单个Docker容器。使用docker_services固定装置的stop方法:

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方法检查Docker服务是否真正启动。默认情况下,它向服务器的/端点发出HTTP GET请求。服务将重试检查,直到超过30秒的超时。

自定义服务检查器

某些服务可能工作方式不同,并需要自定义检查器。

创建一个自定义服务检查器函数,该函数接收IP地址和端口号作为参数:

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

在固定装置中,将自定义服务检查器函数作为check_service参数提供给wait_for_service方法:

    @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

要使用默认检查器的另一个请求路径,可以使用url_checker方法创建另一个路径的check_url方法:

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

运行测试

测试位于tests目录中。运行测试是通过pytest包完成的:

    ./gradlew pytest

发布新版本

使用gradle构建并使用twine上传,如下所示

    ./gradlew sdist
    twine upload build/sdist/lovely-pytest-docker-<version>.tar.gz

注意:twine需要通过其他方式安装(例如,pip install twine

项目详情


下载文件

下载适合您平台的项目文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

lovely_pytest_docker-1.0.0.tar.gz (12.9 kB 查看哈希值)

上传时间

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面