tensorflow_models_learning
tensorflow_models_learning copied to clipboard
feat: Add comprehensive Python testing infrastructure with Poetry
Add Python Testing Infrastructure
Summary
This PR sets up a comprehensive testing infrastructure for the TensorFlow-Slim models project, providing developers with all the tools needed to write and run tests effectively.
Changes Made
Package Management
- Poetry Setup: Configured Poetry as the package manager via
pyproject.toml - Dependency Migration: Migrated from traditional
setup.pyto modern Poetry configuration - Lock File: Poetry will generate a
poetry.lockfile for reproducible builds
Testing Dependencies
Added the following development dependencies:
pytest ^7.4.0- Modern Python testing frameworkpytest-cov ^4.1.0- Coverage reporting plugin for pytestpytest-mock ^3.11.0- Mocking utilities for pytest
Testing Configuration
Configured in pyproject.toml:
- Test Discovery: Configured to find
test_*.pyand*_test.pyfiles - Coverage Settings:
- 80% minimum coverage threshold
- HTML, XML, and terminal coverage reports
- Excludes test files and setup files from coverage
- Custom Markers: Added
unit,integration, andslowmarkers for test categorization - Strict Mode: Enabled strict markers and verbose output
Directory Structure
Created organized testing structure:
tests/
├── __init__.py
├── conftest.py # Shared pytest fixtures
├── unit/
│ └── __init__.py
├── integration/
│ └── __init__.py
└── test_infrastructure_validation.py
Shared Fixtures (conftest.py)
Comprehensive fixtures for common testing needs:
temp_dir- Temporary directory managementmock_image_file- Creates test imagesmock_model_checkpoint- Mock TensorFlow checkpointsmock_tfrecord_file- Mock TFRecord filesmock_labels_file- Label file generationmock_config- Configuration dictionariesmock_dataset_split- Dataset directory structurescapture_logs- Log capture utilitiesmock_env_vars- Environment variable mockingcleanup_files- File cleanup tracking- Auto-reset of TensorFlow graphs between tests
Development Commands
Configured Poetry scripts for convenience:
poetry run test- Run all testspoetry run tests- Alternative command (both work identically)
Both commands support all standard pytest options (e.g., -v, -k, -m, etc.)
Version Control
Added comprehensive .gitignore covering:
- Python build artifacts and caches
- Testing outputs (coverage reports, pytest cache)
- Virtual environments
- IDE files
- TensorFlow specific files (checkpoints, events, models)
- Claude AI settings
Usage Instructions
Initial Setup
# Install Poetry if not already installed
curl -sSL https://install.python-poetry.org | python3 -
# Install dependencies
poetry install
# Run validation tests
poetry run test tests/test_infrastructure_validation.py -v
Running Tests
# Run all tests
poetry run test
# Run with coverage report
poetry run test --cov-report=html
# Run specific test markers
poetry run test -m unit
poetry run test -m integration
poetry run test -m "not slow"
# Run specific test file
poetry run test tests/unit/test_example.py
Writing New Tests
- Create test files following the naming convention:
test_*.pyor*_test.py - Place unit tests in
tests/unit/and integration tests intests/integration/ - Use the provided fixtures from
conftest.pyfor common testing needs - Mark tests appropriately with
@pytest.mark.unit,@pytest.mark.integration, or@pytest.mark.slow
Notes
- The infrastructure is ready for immediate use - developers can start writing tests right away
- Coverage reporting is configured but will show 0% until actual tests are written for the codebase
- The validation test file demonstrates all fixture usage and can serve as a reference
- TensorFlow dependencies are included based on the project requirements
Next Steps
- Developers can now write unit tests for individual components
- Integration tests can be added for end-to-end workflows
- Consider adding pre-commit hooks to run tests automatically
- Set up CI/CD pipeline to run tests on pull requests