Skip to content

Ensure cleanup always runs by utilizing a pytest finalizer. #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/pytest_docker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = (
Expand Down
7 changes: 5 additions & 2 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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',
Expand Down