【发布时间】:2020-01-25 16:55:42
【问题描述】:
我构建了一个在后端使用数据库的应用程序。对于集成测试,我在 Docker 中启动数据库并使用 pytest 运行测试套件。
我使用带有autouse=True 的会话范围固定装置来启动 Docker 容器:
@pytest.fixture(scope='session', autouse=True)
def run_database():
# setup code skipped ...
# start container with docker-py
container.start()
# yield container to run tests
yield container
# stop container afterwards
container.stop()
我使用另一个会话范围的夹具将数据库连接传递给测试函数:
@pytest.fixture(scope='session')
def connection():
return Connection(...)
现在我可以运行一个测试函数了:
def test_something(connection):
result = connection.run(...)
assert result == 'abc'
但是,我想针对多个不同版本的数据库运行我的测试函数。
我可以在 run_database() 夹具中运行多个 Docker 容器。如何参数化我的测试函数,以便它们针对两个不同的 connection() 固定装置运行?
【问题讨论】: