Darkweb-search-engine icon indicating copy to clipboard operation
Darkweb-search-engine copied to clipboard

feat: Add comprehensive Python testing infrastructure

Open llbbl opened this issue 4 months ago • 0 comments

Python Testing Infrastructure Setup

Summary

This PR establishes a complete testing infrastructure for the Python project, providing all the necessary tools and configuration for developers to immediately start writing and running tests.

Changes Made

Package Management

  • Poetry Setup: Configured Poetry as the package manager in pyproject.toml
  • Dependency Management: Prepared for migration from requirements.txt to Poetry (see pyproject_full.toml for full dependency list)
  • Development Dependencies: Added pytest, pytest-cov, and pytest-mock as development dependencies

Testing Configuration

  • pytest Configuration:

    • Test discovery patterns for test_*.py and *_test.py files
    • Strict markers and configuration enforcement
    • Verbose output with detailed failure information
  • Coverage Settings:

    • 80% minimum coverage threshold
    • HTML and XML report generation
    • Branch coverage tracking
    • Exclusion of test files and boilerplate code
  • Custom Markers:

    • unit: For isolated component tests
    • integration: For multi-component tests
    • slow: For long-running tests (can be excluded with -m "not slow")

Directory Structure

tests/
├── __init__.py
├── conftest.py              # Shared fixtures and configuration
├── test_setup_validation.py # Infrastructure validation tests
├── unit/                    # Unit tests directory
│   └── __init__.py
└── integration/            # Integration tests directory
    └── __init__.py

Test Fixtures (conftest.py)

Comprehensive fixtures for common testing needs:

  • File/Directory Management: temp_dir, temp_file, sample_html_file
  • Configuration: test_config, env_vars
  • Mock Objects: mock_database, mock_elasticsearch, mock_tor_session
  • Sample Data: sample_onion_data, sample_bitcoin_addresses, sample_email_addresses
  • Network Mocks: mock_port_scanner, mock_whatweb_output
  • Utilities: assert_helpers, benchmark_timer, frozen_time
  • Cleanup: Automatic test file cleanup

Development Commands

  • poetry run test - Run all tests
  • poetry run tests - Alternative command (both work)
  • Standard pytest options are available (e.g., -v, -k, -m)

.gitignore Updates

Added entries for:

  • Testing artifacts (.pytest_cache/, .coverage, htmlcov/, coverage.xml)
  • Claude settings (.claude/*)
  • Build artifacts and virtual environments
  • IDE files and temporary files

How to Use

Installation

# Install Poetry if not already installed
pipx install poetry

# Install dependencies including dev dependencies
poetry install

# Run validation tests
poetry run pytest tests/test_setup_validation.py -v

Running Tests

# Run all tests with coverage
poetry run test

# Run only unit tests
poetry run test -m unit

# Run tests without coverage
poetry run test --no-cov

# Run specific test file
poetry run test tests/test_something.py

# Run with specific verbosity
poetry run test -vv

Writing New Tests

  1. Create test files in tests/unit/ or tests/integration/
  2. Use fixtures from conftest.py for common needs
  3. Mark tests appropriately (@pytest.mark.unit, @pytest.mark.integration, @pytest.mark.slow)
  4. Follow naming convention: test_*.py for test files, test_* for test functions

Validation

The infrastructure has been validated with 13 passing tests that verify:

  • Project modules can be imported
  • All fixtures are working correctly
  • Custom markers are properly defined
  • Coverage configuration is set up
  • Python path includes project root

Next Steps

  1. Migrate existing dependencies from requirements.txt to Poetry (use pyproject_full.toml as reference)
  2. Start writing unit tests for existing modules
  3. Set up CI/CD pipeline to run tests automatically
  4. Consider adding additional testing tools (e.g., tox for multiple Python versions, black for formatting)

Notes

  • The testing infrastructure is ready to use immediately
  • No actual unit tests for the codebase were written - only infrastructure setup
  • Coverage threshold is set to 80% but can be adjusted in pyproject.toml
  • The setup supports both local development and CI/CD integration

llbbl avatar Aug 22 '25 22:08 llbbl