diff --git a/tests/conftest.py b/tests/conftest.py index 5addc584..2e8877f7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,5 @@ import os +import subprocess import pytest from celery import Celery @@ -45,3 +46,31 @@ def default_worker_app(default_worker_app: Celery) -> Celery: if app.conf.broker_url and app.conf.broker_url.startswith("sqs"): app.conf.broker_transport_options["region"] = LOCALSTACK_CREDS["AWS_DEFAULT_REGION"] return app + + +@pytest.fixture(scope="module", autouse=True) +def auto_clean_docker_resources(): + """Clean up docker resources after each test module.""" + + def run_shell_command(command): + try: + subprocess.run( + command, + shell=True, + check=False, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + except Exception: + pass + + docker_cleanup_commands = [ + "containers=$(docker ps -aq --filter label=creator=pytest-docker-tools); " + 'if [ -n "$containers" ]; then docker rm -f $containers; fi', + "networks=$(docker network ls --filter name=pytest- -q); " + 'if [ -n "$networks" ]; then docker network rm $networks; fi', + "volumes=$(docker volume ls --filter name=pytest- -q); " + 'if [ -n "$volumes" ]; then docker volume rm $volumes; fi', + ] + for command in docker_cleanup_commands: + run_shell_command(command) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index c667844f..527f316d 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -55,3 +55,8 @@ def default_redis_broker() -> RedisContainer: @pytest.fixture def default_worker_container() -> CeleryWorkerContainer: return mocked_container(CeleryWorkerContainer) + + +@pytest.fixture(scope="module", autouse=True) +def auto_clean_docker_resources(): + """Skip cleanup in the unit tests."""