55import subprocess # noqa: S404
66import time
77import timeit
8+ from contextlib import AbstractContextManager
89from typing import TYPE_CHECKING , Any , Callable , Iterable
910
1011from pytest_databases .helpers import simple_string_hash
1112
1213if TYPE_CHECKING :
1314 from collections .abc import Awaitable , Generator
1415 from pathlib import Path
16+ from types import TracebackType
1517
1618TRUE_VALUES = {"True" , "true" , "1" , "yes" , "Y" , "T" }
1719
@@ -47,7 +49,7 @@ def wait_until_responsive(
4749COMPOSE_PROJECT_NAME : str = f"pytest-databases-{ simple_string_hash (__file__ )} "
4850
4951
50- class DockerServiceRegistry :
52+ class DockerServiceRegistry ( AbstractContextManager ) :
5153 def __init__ (
5254 self ,
5355 worker_id : str ,
@@ -65,6 +67,15 @@ def __init__(
6567 )
6668 self ._before_start = list (before_start ) if before_start else []
6769
70+ def __exit__ (
71+ self ,
72+ / ,
73+ __exc_type : type [BaseException ] | None ,
74+ __exc_value : BaseException | None ,
75+ __traceback : TracebackType | None ,
76+ ) -> None :
77+ self .down ()
78+
6879 @staticmethod
6980 def _get_docker_ip () -> str :
7081 docker_host = os .environ .get ("DOCKER_HOST" , "" ).strip ()
@@ -114,4 +125,4 @@ def stop(self, name: str) -> None:
114125
115126 def down (self ) -> None :
116127 if not SKIP_DOCKER_COMPOSE :
117- self .run_command ("down" , "-t" , "10" )
128+ self .run_command ("down" , "-t" , "10" , "--volumes" )
0 commit comments