Make a test fixture available for the duration of the entire pytest session
when you have the following in your conftest.py
.
Assuming you have a docker-compose.yml
in containers/docker/docker-compose.yml
.
# Third Party
import pytest
from testcontainers.compose import DockerCompose
@pytest.fixture(name="dockercompose", scope="session")
def _docker_compose():
with DockerCompose(context="containers/docker/", compose_file_name="docker-compose.yml", pull=True, build=True) as compose:
compose.wait_for("http://localhost:8080/")
yield compose
And here is an example test using it:
# Third Party
import pytest
import aiohttp
import base64
HOST = "http://localhost:8080/"
BASIC_AUTH = base64.b64encode("testuser:insecurepassword".encode()).decode()
headers={"Authorization": f"Basic {BASIC_AUTH}"}
@pytest.mark.asyncio
@pytest.mark.docker
async def test_example(dockercompose) -> None:
"""Start docker compose fixture and run endpoint check."""
print(headers)
async with aiohttp.ClientSession() as session:
async with session.get(HOST, headers=headers) as response:
data = await response.json()
assert len(data) > 0
Since this test is mark
ed as docker
with a custom marker you can use the marks selector from pytest
. Eg
python3 -m pytest -m docker