testcontainers / testcontainers-python

Testcontainers is a Python library that providing a friendly API to run Docker container. It is designed to create runtime environment to use during your automatic tests.
https://testcontainers-python.readthedocs.io/en/latest/
Apache License 2.0
1.44k stars 270 forks source link

feat(core): DockerCompose.stop now stops only services that it starts (does not stop the other services) #620

Closed aksel closed 3 weeks ago

aksel commented 3 weeks ago

The command would otherwise stop/down all services, not just the services the instance itself started.

Useful for e.g. one fixture per service, and you want different scopes for the services.

aksel commented 3 weeks ago

Apologies if my PR breaks any conventions or contribution guidelines. I was a bit confused by the docs, as they seemed to be targeted towards contributing features, which I don't think this is.

Let me know if I should close, resubmit, or change anything

Thanks! :smile:

aksel commented 3 weeks ago

My specific use case is this:

import pytest
from testcontainers.compose import DockerCompose

@pytest.fixture(scope="session")
async def aws() -> DockerCompose:
    with DockerCompose(
        ".",
        compose_file_name="docker-compose.yaml",
        pull=True,
        build=True,
        services=["aws"],
        # Wait for localstack to be healthy
        wait=True,
    ) as compose:
        yield compose

@pytest.fixture(scope="module")
async def db(aws) -> DockerCompose:
    with DockerCompose(
        ".",
        compose_file_name="docker-compose.yaml",
        pull=True,
        build=True,
        # Only start the DB
        services=["db"],
        # Wait for the DB to be healthy
        wait=True,
    ) as compose:
        yield compose

Starting up aws takes a while, maybe around 20 seconds, but it can be session scoped and only done once. Until now, both have been session scoped. But now I want to get a clean DB in between modules (or packages, more likely). Nicer isolation and all that. But I couldn't figure out a way to stop only db, while keeping aws running. At first glance, seemed like an oversight, so I thought I might as well submit a PR with the two-line change.

codecov[bot] commented 3 weeks ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Please upload report for BASE (main@27f2a6b). Learn more about missing BASE report.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #620 +/- ## ======================================= Coverage ? 80.29% ======================================= Files ? 11 Lines ? 604 Branches ? 86 ======================================= Hits ? 485 Misses ? 92 Partials ? 27 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.