Skip to content

feat: Set up comprehensive Python testing infrastructure#4

Open
llbbl wants to merge 1 commit intobenjaminchodroff:masterfrom
UnitSeeker:add-testing-infrastructure
Open

feat: Set up comprehensive Python testing infrastructure#4
llbbl wants to merge 1 commit intobenjaminchodroff:masterfrom
UnitSeeker:add-testing-infrastructure

Conversation

@llbbl
Copy link

@llbbl llbbl commented Sep 2, 2025

Python Testing Infrastructure Setup

Summary

This PR establishes a complete testing infrastructure for the alarm-beep-detect Python project. The setup provides a professional development environment with proper dependency management, comprehensive testing configuration, and ready-to-use fixtures for audio-related testing.

Key Changes Made

  • Package Management: Migrated from requirements.txt to Poetry with pyproject.toml
  • Testing Framework: Configured pytest with coverage reporting (80% threshold)
  • Directory Structure: Created organized test directories (tests/unit/, tests/integration/)
  • Shared Fixtures: Added comprehensive conftest.py with audio testing utilities
  • Development Tools: Added pytest-cov, pytest-mock, and custom test markers
  • Configuration: Set up proper .gitignore and pytest configuration
  • Validation: Included tests to verify the infrastructure works correctly

Testing Infrastructure Components

Package Management (Poetry)

  • pyproject.toml: Central configuration for dependencies, scripts, and tools
  • Dependencies: Core packages (numpy) and optional audio (PyAudio) support
  • Dev Dependencies: pytest, pytest-cov, pytest-mock for comprehensive testing

Test Configuration

  • Coverage: 80% threshold with HTML/XML reporting in htmlcov/ and coverage.xml
  • Markers: Custom markers for unit, integration, and slow tests
  • Discovery: Automatic test discovery with standard naming patterns
  • Output: Detailed reporting with missing coverage information

Shared Fixtures (conftest.py)

  • File Operations: temp_dir, temp_file for filesystem testing
  • Audio Testing: mock_pyaudio, sample_audio_data, sample_noise_data
  • Configuration: mock_audio_config, mock_alarm_config for consistent test setup
  • Environment: Automatic test environment setup and audio warning suppression

Directory Structure

tests/
├── __init__.py
├── conftest.py              # Shared fixtures and configuration
├── test_setup_validation.py # Infrastructure validation tests
├── unit/                    # Fast, isolated unit tests
│   └── __init__.py
└── integration/             # Slower integration tests
    └── __init__.py

Running Tests

Basic test execution:

# Run all tests
poetry run pytest

# Run with coverage
poetry run pytest --cov

# Run specific test types
poetry run pytest -m unit        # Unit tests only
poetry run pytest -m integration # Integration tests only
poetry run pytest -m slow        # Slow tests only

Alternative commands:

poetry run test    # Equivalent to pytest
poetry run tests   # Alternative command

Coverage reporting:

  • Terminal output with missing lines
  • HTML report in htmlcov/index.html
  • XML report in coverage.xml for CI/CD integration

Development Notes

  • PyAudio: Made optional to support environments without audio hardware
    • Install with: poetry install --extras audio
    • Mocked in tests for consistent behavior
  • Python Version: Requires Python 3.9+ for numpy compatibility
  • Coverage: Configured to exclude test files, build artifacts, and virtual environments
  • Validation: Run poetry run pytest tests/test_setup_validation.py to verify setup

Next Steps

The testing infrastructure is now ready for developers to:

  1. Write Unit Tests: Add tests in tests/unit/ for individual functions
  2. Add Integration Tests: Create tests in tests/integration/ for complete workflows
  3. Use Fixtures: Leverage audio-specific fixtures for consistent test setup
  4. Monitor Coverage: Maintain 80% coverage threshold with detailed reporting
  5. Run CI/CD: Use coverage.xml and junit-xml output for automated testing

This foundation provides everything needed to maintain high code quality through comprehensive testing.

- Configure Poetry package manager with pyproject.toml
- Migrate dependencies from requirements.txt to Poetry
- Add pytest, pytest-cov, and pytest-mock as dev dependencies
- Create structured test directory (tests/unit/, tests/integration/)
- Configure pytest with 80% coverage threshold and custom markers
- Add comprehensive conftest.py with shared fixtures for audio testing
- Include validation tests to verify infrastructure functionality
- Update .gitignore with testing and development artifacts
- Set up Poetry scripts for running tests (poetry run test/tests)
- Make PyAudio optional to support environments without audio hardware
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant