Skip to content

feat: Auto-cleanup test containers on failure with --keep-containers flag #18

@gerchowl

Description

@gerchowl

Problem

Test containers are left behind when tests fail because fixture cleanup doesn't always run on hard failures or interrupts. This causes:

  • Resource accumulation
  • make test failures due to lingering container detection
  • Manual cleanup required after debugging

Proposed Solution

1. Add pytest option --keep-containers

def pytest_addoption(parser):
    parser.addoption(
        "--keep-containers",
        action="store_true",
        default=False,
        help="Keep test containers after test completion (for debugging failed tests)",
    )

2. Use request.addfinalizer() for reliable cleanup

Replace yield-based cleanup with addfinalizer() which runs even on test failures:

@pytest.fixture(scope="function")
def devcontainer_up(initialized_workspace, request):
    # ... setup code ...
    
    def cleanup():
        if request.config.getoption("--keep-containers"):
            print(f"\n⚠️  Keeping container for debugging: {container_name}")
            return
        # ... existing cleanup code (podman compose down, etc.) ...
    
    request.addfinalizer(cleanup)
    
    yield workspace_path

Usage

# Normal run - auto-cleanup on success AND failure
make test-integration

# Debug mode - keep containers for investigation  
uv run pytest tests/test_integration.py --keep-containers -x

# Inspect the failed container
podman exec -it <container_name> bash

# Clean up manually when done
make clean-test-containers

Benefits

  • Containers cleaned up reliably even on failures
  • --keep-containers flag for debugging
  • Works with -x (stop on first failure) for debugging
  • No more manual cleanup after failed test runs

Affected Fixtures

  • devcontainer_up
  • devcontainer_with_sidecar
  • initialized_workspace
  • Any other fixtures that create containers

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions