diff --git a/src/pytest_docker/__init__.py b/src/pytest_docker/__init__.py index 567ec9f..4be445c 100644 --- a/src/pytest_docker/__init__.py +++ b/src/pytest_docker/__init__.py @@ -140,22 +140,23 @@ def docker_compose_project_name(): @pytest.fixture(scope='session') def docker_services( - docker_compose_file, docker_compose_project_name + request, docker_compose_file, docker_compose_project_name ): """Ensure all Docker-based services are up and running.""" + def _cleanup(): + docker_compose.execute('down -v') docker_compose = DockerComposeExecutor( docker_compose_file, docker_compose_project_name ) + # If failure happens beyond this point, run cleanup + request.addfinalizer(_cleanup) # Spawn containers. docker_compose.execute('up --build -d') # Let test(s) run. - yield Services(docker_compose) - - # Clean up. - docker_compose.execute('down -v') + return Services(docker_compose) __all__ = ( diff --git a/tests/test_integration.py b/tests/test_integration.py index b4c02b9..6492afe 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -39,7 +39,8 @@ def test_main_fixtures_work(docker_ip, docker_services): assert response.status_code == 204 -def test_containers_and_volumes_get_cleaned_up(testdir, tmpdir, docker_compose_file): +def test_containers_and_volumes_get_cleaned_up(testdir, tmpdir, + docker_compose_file): _copy_compose_files_to_testdir(testdir, docker_compose_file) project_name_file_path = path.join(str(tmpdir), 'project_name.txt') @@ -80,7 +81,9 @@ def _copy_compose_files_to_testdir(testdir, compose_file_path): directory_for_compose_files = testdir.mkdir('tests') shutil.copy(compose_file_path, str(directory_for_compose_files)) - container_build_files_dir = path.realpath(path.join(compose_file_path, '../containers')) + container_build_files_dir = path.realpath( + path.join(compose_file_path, '../containers') + ) shutil.copytree( container_build_files_dir, str(directory_for_compose_files) + '/containers',