Darkweb-search-engine
Darkweb-search-engine copied to clipboard
feat: Add comprehensive Python testing infrastructure
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.txtto Poetry (seepyproject_full.tomlfor full dependency list) - Development Dependencies: Added pytest, pytest-cov, and pytest-mock as development dependencies
Testing Configuration
-
pytest Configuration:
- Test discovery patterns for
test_*.pyand*_test.pyfiles - Strict markers and configuration enforcement
- Verbose output with detailed failure information
- Test discovery patterns for
-
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 testsintegration: For multi-component testsslow: 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 testspoetry 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
- Create test files in
tests/unit/ortests/integration/ - Use fixtures from
conftest.pyfor common needs - Mark tests appropriately (
@pytest.mark.unit,@pytest.mark.integration,@pytest.mark.slow) - Follow naming convention:
test_*.pyfor 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
- Migrate existing dependencies from
requirements.txtto Poetry (usepyproject_full.tomlas reference) - Start writing unit tests for existing modules
- Set up CI/CD pipeline to run tests automatically
- 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