FigmaChain icon indicating copy to clipboard operation
FigmaChain copied to clipboard

feat: Set up comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 6 months ago • 0 comments

Set Up Python Testing Infrastructure

Summary

This PR establishes a complete testing infrastructure for the Python project using Poetry as the package manager and pytest as the testing framework. The setup provides a solid foundation for writing and running tests with comprehensive coverage reporting.

Changes Made

Package Management

  • Poetry Configuration: Created pyproject.toml with Poetry setup
  • Dependency Migration: Migrated all dependencies from requirements.txt to Poetry
  • Python Version: Configured Python compatibility (>=3.8.1, excluding 3.9.7 per Streamlit requirements)

Testing Framework

  • pytest: Added as the primary testing framework
  • pytest-cov: Added for coverage reporting
  • pytest-mock: Added for mocking utilities
  • All testing dependencies are properly isolated as development dependencies

Testing Configuration

pytest Configuration (pyproject.toml):

  • Test discovery patterns for flexible test file naming
  • Coverage reporting with multiple formats (terminal, HTML, XML)
  • Custom markers for test categorization:
    • @pytest.mark.unit - Fast, isolated unit tests
    • @pytest.mark.integration - Integration tests with dependencies
    • @pytest.mark.slow - Long-running tests
  • Strict mode enabled for better error detection
  • Coverage threshold initially set to 0% (should be increased as tests are added)

Directory Structure

tests/
├── __init__.py
├── conftest.py          # Shared pytest fixtures
├── test_setup_validation.py  # Validation tests
├── unit/
│   └── __init__.py
└── integration/
    └── __init__.py

Shared Fixtures (conftest.py)

  • temp_dir: Temporary directory for file operations
  • mock_env_vars: Environment variable mocking
  • sample_config: Sample configuration for testing
  • mock_api_response: Mock API response structure
  • isolated_filesystem: Isolated filesystem for safe file operations
  • mock_streamlit: Streamlit component mocking

Build Configuration

  • Updated .gitignore with testing artifacts and Poetry files
  • Poetry lock file is tracked (not ignored) for reproducible builds

How to Use

Install Dependencies

poetry install

Run Tests

Both commands work:

poetry run test
poetry run tests

Run Tests with Options

# Run only unit tests
poetry run pytest -m unit

# Run with verbose output
poetry run pytest -v

# Run specific test file
poetry run pytest tests/test_setup_validation.py

# Generate coverage report
poetry run pytest --cov

Coverage Reports

  • Terminal: Displayed after each test run
  • HTML: Generated in htmlcov/ directory
  • XML: Generated as coverage.xml for CI integration

Next Steps

  1. Increase coverage threshold in pyproject.toml as tests are added (currently 0%)
  2. Write unit tests for existing modules (chatbot.py, generateCode.py)
  3. Add integration tests for API interactions
  4. Configure CI/CD to run tests automatically

Validation

The setup includes validation tests that verify:

  • All testing packages are properly installed
  • Directory structure is correctly created
  • Configuration files exist and are valid
  • Custom markers work as expected
  • Shared fixtures are accessible

All validation tests pass successfully, confirming the infrastructure is ready for use.

llbbl avatar Jun 20 '25 15:06 llbbl