tensorflow_models_learning icon indicating copy to clipboard operation
tensorflow_models_learning copied to clipboard

feat: Add comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 6 months ago • 0 comments

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.py to modern Poetry configuration
  • Lock File: Poetry will generate a poetry.lock file for reproducible builds

Testing Dependencies

Added the following development dependencies:

  • pytest ^7.4.0 - Modern Python testing framework
  • pytest-cov ^4.1.0 - Coverage reporting plugin for pytest
  • pytest-mock ^3.11.0 - Mocking utilities for pytest

Testing Configuration

Configured in pyproject.toml:

  • Test Discovery: Configured to find test_*.py and *_test.py files
  • 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, and slow markers 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 management
  • mock_image_file - Creates test images
  • mock_model_checkpoint - Mock TensorFlow checkpoints
  • mock_tfrecord_file - Mock TFRecord files
  • mock_labels_file - Label file generation
  • mock_config - Configuration dictionaries
  • mock_dataset_split - Dataset directory structures
  • capture_logs - Log capture utilities
  • mock_env_vars - Environment variable mocking
  • cleanup_files - File cleanup tracking
  • Auto-reset of TensorFlow graphs between tests

Development Commands

Configured Poetry scripts for convenience:

  • poetry run test - Run all tests
  • poetry 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

  1. Create test files following the naming convention: test_*.py or *_test.py
  2. Place unit tests in tests/unit/ and integration tests in tests/integration/
  3. Use the provided fixtures from conftest.py for common testing needs
  4. 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

  1. Developers can now write unit tests for individual components
  2. Integration tests can be added for end-to-end workflows
  3. Consider adding pre-commit hooks to run tests automatically
  4. Set up CI/CD pipeline to run tests on pull requests

llbbl avatar Jun 25 '25 16:06 llbbl