WeblogicScan icon indicating copy to clipboard operation
WeblogicScan 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 comprehensive testing infrastructure for the WebLogic scanner 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 as the project's package manager
  • Dependency Migration: Migrated existing requests dependency from requirements.txt to Poetry
  • Development Dependencies: Added pytest, pytest-cov, and pytest-mock as development dependencies

Testing Configuration

  • pytest Configuration: Configured pytest in pyproject.toml with:

    • Test discovery patterns for test_*.py and *_test.py files
    • Coverage reporting with 80% threshold requirement
    • HTML and XML coverage report generation
    • Strict markers and configuration options
    • Custom test markers: unit, integration, and slow
  • Coverage Configuration: Set up coverage.py with:

    • Source directory targeting the app module
    • Branch coverage enabled
    • Exclusion patterns for test files and common Python patterns
    • HTML reports in htmlcov/ and XML report as coverage.xml

Project Structure

  • Created proper testing directory structure:
    tests/
    ├── __init__.py
    ├── conftest.py
    ├── test_setup_validation.py
    ├── unit/
    │   └── __init__.py
    └── integration/
        └── __init__.py
    

Shared Fixtures

Created conftest.py with commonly needed pytest fixtures:

  • temp_dir: Temporary directory for file operations
  • mock_config: Mock configuration dictionary
  • mock_response: Mock HTTP response object
  • mock_requests: Mock requests module
  • test_data_dir: Path to test data directory
  • sample_url: Sample WebLogic URL
  • sample_payload: Sample test payload
  • reset_environment: Auto-reset environment variables
  • capture_logs: Log capturing fixture
  • mock_file_system: Mock file system structure

Additional Setup

  • Updated .gitignore: Added comprehensive entries for:

    • Python artifacts and caches
    • Testing artifacts (.pytest_cache/, .coverage, htmlcov/, etc.)
    • Virtual environments
    • IDE files
    • Claude Code settings (.claude/*)
    • Note: Poetry lock file is NOT ignored (as per best practices)
  • Poetry Scripts: Configured both poetry run test and poetry run tests commands to run pytest

  • Validation Tests: Created test_setup_validation.py to verify:

    • pytest installation and configuration
    • Project structure integrity
    • Module importability
    • Fixture availability
    • Marker functionality
    • Coverage configuration
    • Python path setup

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 report:

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

Notes

  • The coverage threshold is set to 80% but can be adjusted in pyproject.toml
  • Currently, the project's actual coverage is below the threshold (36.02%), which is expected since this PR only sets up the infrastructure
  • Developers can now immediately start writing unit and integration tests using this infrastructure
  • All test dependencies are isolated as development dependencies and won't affect production deployments

llbbl avatar Jun 23 '25 23:06 llbbl