Skip to content

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jun 26, 2025

Set up Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the MangoDB server project using modern Python development tools and best practices.

Changes Made

Package Management

  • Migrated to Poetry: Created pyproject.toml with Poetry configuration
  • Updated dependencies: Migrated from requirements.txt to Poetry dependencies
  • Updated gevent version: Upgraded from 0.13.7 to 23.9.0 for Python 3.x compatibility

Testing Framework

  • Added testing dependencies:
    • pytest - Main testing framework
    • pytest-cov - Coverage reporting with 80% threshold
    • pytest-mock - Mocking utilities for unit tests

Project Structure

tests/
├── __init__.py
├── conftest.py          # Shared fixtures
├── test_setup_validation.py  # Infrastructure validation
├── unit/
│   ├── __init__.py
│   └── test_server_basic.py  # Basic example tests
└── integration/
    └── __init__.py

Configuration

  • pytest configuration in pyproject.toml:

    • Test discovery patterns
    • Coverage settings (80% threshold, HTML/XML reports)
    • Custom markers: unit, integration, slow
    • Strict mode with verbose output
  • Coverage configuration:

    • Source: server.py
    • Excludes: test files, virtual environments, build artifacts
    • Reports: terminal, HTML (htmlcov/), XML (coverage.xml)

Shared Fixtures (conftest.py)

  • temp_dir - Temporary directory management
  • temp_file - Temporary file handling
  • mock_socket - Socket mocking for network tests
  • mock_environ - Environment variable mocking
  • mock_gevent_server - Gevent server mocking
  • mock_file_operations - File I/O mocking
  • mock_threading - Threading component mocking
  • reset_modules - Module state reset between tests
  • client_socket_data - Sample socket communication data
  • server_config - Test server configuration

Additional Files

  • Updated .gitignore: Added testing artifacts, build files, IDE files, and .claude/*
  • Created run_tests.sh: Helper script for running tests

How to Use

Install dependencies

poetry install

Run all tests

poetry run pytest
# or
./run_tests.sh

Run specific test types

# Unit tests only
poetry run pytest -m unit

# Integration tests only
poetry run pytest -m integration

# Exclude slow tests
poetry run pytest -m "not slow"

Run with coverage

# Coverage is enabled by default
poetry run pytest

# View HTML coverage report
open htmlcov/index.html

Run validation tests

poetry run pytest tests/test_setup_validation.py -v

Notes

  • The project uses Poetry in non-package mode since it's a single-file application
  • Coverage threshold is set to 80% - tests will fail if coverage drops below this
  • The infrastructure includes validation tests to ensure all components work correctly
  • Example basic tests are provided in tests/unit/test_server_basic.py to demonstrate usage

Next Steps

Developers can now immediately start writing tests for the MangoDB server functionality using the established infrastructure and shared fixtures.

- Migrate from requirements.txt to Poetry package management
- Add pytest, pytest-cov, and pytest-mock as dev dependencies
- Configure pytest with coverage reporting (80% threshold)
- Create test directory structure (unit/integration)
- Add shared fixtures in conftest.py
- Update .gitignore with testing artifacts
- Add validation tests to verify setup
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