Fixed the anti-pattern where tests were persisting data to the actual data/resumes and data/job_listings directories instead of using isolated temporary directories.
Status: ✅ Complete - All 401 tests pass with proper isolation
- Tests in
test_multi_resume_api.pyused the globalresume_modelandjob_listing_modelfromapp.py - These models wrote directly to
data/resumes/anddata/job_listings/directories - Test data persisted between test runs, causing:
- Accumulation of test files in production directories
- Potential conflicts when tests run multiple times
- Difficulty in reproducing test failures
- Violation of test isolation principles
- 29 test resume files accumulated in
data/resumes/directory - Similar accumulation in
data/job_listings/directory - Tests relied on cleanup code that wasn't always executed
Centralized pytest fixtures for test isolation:
@pytest.fixture
def temp_data_dir():
"""Create a temporary data directory for testing."""
temp_dir = Path(tempfile.mkdtemp())
yield temp_dir
shutil.rmtree(temp_dir, ignore_errors=True)
@pytest.fixture
def resume_model(temp_data_dir):
"""Create isolated Resume model with temporary directory."""
return Resume(temp_data_dir)
@pytest.fixture
def job_listing_model(temp_data_dir):
"""Create isolated JobListing model with temporary directory."""
return JobListing(temp_data_dir)
@pytest.fixture
def unique_resume_name():
"""Generate unique resume name to prevent conflicts."""
import time
timestamp = int(time.time() * 1000)
return f"Test_Resume_{timestamp}"- Replaced global model usage with isolated fixtures
- Updated
clientfixture to patch app models with temporary directory versions - Used
monkeypatchto safely patch module-level variables - Removed manual cleanup code (automatic via fixture cleanup)
- Updated all test methods to use unique names
Key Changes:
@pytest.fixture
def client(temp_data_dir, monkeypatch):
"""Create test client with isolated data directory."""
from api import app as app_module
from models.resume import Resume
from models.job_listing import JobListing
# Patch app module to use temporary directory
monkeypatch.setattr(app_module, "DATA_DIR", temp_data_dir)
monkeypatch.setattr(app_module, "resume_model", Resume(temp_data_dir))
monkeypatch.setattr(app_module, "job_listing_model", JobListing(temp_data_dir))
app.config["TESTING"] = True
with app.test_client() as test_client:
yield test_client- Test Isolation: Each test runs in its own temporary directory
- No Data Persistence: Test data is automatically cleaned up after each test
- Parallel Test Execution: Tests can run in parallel without conflicts
- Reproducibility: Tests produce consistent results regardless of previous runs
- Cleaner Production Directories: No accumulation of test files
- 29 test resume files in
data/resumes/ - Manual cleanup required
- Risk of test data conflicts
====================== 401 passed, 3 warnings in 24.81s =======================
- ✅ All 401 tests pass
- ✅ No new test data persists in
data/resumes/ - ✅ No new test data persists in
data/job_listings/ - ✅ Automatic cleanup via fixtures
-
tests/conftest.py (NEW)
- Centralized pytest fixtures
- Shared fixtures for all tests
- Proper cleanup with
shutil.rmtree
-
tests/test_multi_resume_api.py
- Updated
clientfixture to use isolated models - Updated all test methods to use unique names
- Removed manual cleanup code
- Added documentation about test isolation
- Updated
- Fixture Scope: Used function-scoped fixtures for complete isolation
- Monkeypatch: Used pytest's
monkeypatchfor safe patching - Unique Names: Generated unique names using timestamps
- Automatic Cleanup: Leveraged fixture cleanup for automatic teardown
- Documentation: Added docstrings explaining isolation approach
- Consider using
pytest-factoryboyfor more complex fixtures - Add fixture for other test files that might have similar issues
- Document fixture usage in test guidelines
- Consider database fixtures for integration tests
- Issue #39: Fix Tests persisted data/resumes (anti-pattern)
- Issue #6: Multi-resume support
- Issue #19: Duplicate resume names
Run tests to verify isolation:
# Run all tests
python -m pytest --tb=short -q
# Run specific test file
python -m pytest tests/test_multi_resume_api.py -v
# Check no new files in data/resumes
ls data/resumes/ | wc -lAll tests pass with proper isolation and no data persistence.