Skip to content

Set up Python testing infrastructure with Poetry and pytest#58

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

Set up Python testing infrastructure with Poetry and pytest#58
llbbl wants to merge 1 commit into
maldevel:masterfrom
UnitSeeker:add-testing-infrastructure

Conversation

@llbbl

@llbbl llbbl commented Jun 26, 2025

Copy link
Copy Markdown

Set up Python Testing Infrastructure

Summary

This PR establishes a complete testing infrastructure for the IP Geolocation Python project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Poetry Setup: Created pyproject.toml with Poetry configuration
  • Dependency Migration: Migrated existing dependencies from requirements.txt (termcolor, colorama)
  • Development Dependencies: Added pytest, pytest-cov, and pytest-mock as dev dependencies

Testing Configuration

  • pytest Configuration:

    • Configured test discovery patterns
    • Set up coverage reporting with 80% threshold
    • Added custom markers: unit, integration, and slow
    • Enabled strict mode and detailed output formatting
  • Coverage Settings:

    • Source directories: core and ipgeolocation
    • HTML and XML report generation
    • Excluded test files and virtual environments from coverage

Project Structure

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

Test Fixtures (conftest.py)

  • temp_dir: Temporary directory management
  • temp_file: Temporary file creation
  • mock_config: Mock configuration object
  • mock_response: Mock HTTP response
  • sample_ip_data: Sample geolocation data
  • mock_logger: Mock logger with all methods
  • mock_file_operations: Mock file I/O
  • capture_stdout: Stdout capture utility
  • mock_requests: Mock requests library
  • mock_environment: Environment variable mocking

Development Experience

  • Test Commands: Both poetry run test and poetry run tests work
  • Validation: Created validation tests to verify the infrastructure works correctly
  • GitIgnore: Updated with testing artifacts, virtual environments, and IDE files

How to Use

  1. Install dependencies:

    poetry install
  2. Run tests:

    poetry run test
    # or
    poetry run tests
  3. Run specific test markers:

    poetry run pytest -m unit          # Run only unit tests
    poetry run pytest -m integration   # Run only integration tests
    poetry run pytest -m "not slow"    # Skip slow tests
  4. View coverage reports:

    • Terminal: Automatically shown after test run
    • HTML: Open htmlcov/index.html in browser
    • XML: Available at coverage.xml for CI/CD integration

Notes

  • The coverage threshold is set to 80% for production code
  • Poetry lock file (poetry.lock) is intentionally NOT gitignored
  • The infrastructure is ready for immediate test development
  • All pytest options are available through the configured commands

Next Steps

Developers can now immediately start writing unit and integration tests in the appropriate directories. The comprehensive fixture collection in conftest.py provides common testing utilities out of the box.

- Add Poetry configuration with pyproject.toml
- Configure pytest with coverage reporting and custom markers
- Create testing directory structure (tests/unit, tests/integration)
- Add comprehensive test fixtures in conftest.py
- Include validation tests to verify setup
- Update .gitignore with testing and development entries
- Set coverage threshold to 80% for future development
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