基于Docker容器的Pytest测试实用工具。
项目描述
可爱的Pytest Docker
创建基于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的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 7283abfe400c31ecc7155f9338c6f5af476f2ab506e1aadb9f7e9a5005e491d6 |
|
MD5 | 07404383fe03ada83b1065229bdb70a2 |
|
BLAKE2b-256 | 44eef0093b5b13ea0726e2a9e44da30b9018ab512d38728d84a879f427dc9ea4 |