mvp-ai's picture
|
download
raw
5.17 kB

Testing Guide for AI Agents

This document explains how testing works in BoxPwnr so AI agents can effectively run and write tests.

Test Structure

tests/
├── core/           # Core functionality (LLM manager, strategies, cost tracking)
├── e2e/            # End-to-end tests (full BoxPwnr workflow)
├── executors/      # Docker and tmux executor tests
├── platforms/      # Platform-specific tests (HTB, PortSwigger, etc.)
├── reporting/      # Report generation, attempt analysis
├── replayer/       # Replay functionality tests
├── strategies/     # LLM strategy tests
├── tools/          # Tool tests (Python code tool, etc.)
└── conftest.py     # Shared fixtures and configuration

Pytest Markers

Tests use markers to categorize them. Key markers:

Marker Description Example
requires_api Needs LLM API keys (OpenRouter, OpenAI, Anthropic) LLM integration tests
slow Takes a long time to run Full conversation tests
docker Requires Docker to be running Executor tests
e2e End-to-end tests Full solve attempts

Adding the requires_api marker

If your test makes real LLM API calls, add this at the top of the file:

import pytest

# Mark all tests in this module as requiring API keys
pytestmark = pytest.mark.requires_api

Running Tests

Run all tests

uv run pytest

Run tests for changed files (compares against main branch)

uv run python scripts/pytest_changed.py
# Or use make:
make test-changed

Run tests by marker

# Tests that don't need API keys
uv run pytest -m "not requires_api"

# Tests that need API keys
uv run pytest -m "requires_api"

# Skip slow tests
uv run pytest -m "not slow"

# Combine markers
uv run pytest -m "not requires_api and not slow"

Run with free models (for API tests without paid keys)

uv run pytest -m "requires_api" --free-models-only

How CI Works

CI runs in two phases:

  1. Tests without API keys: pytest -m "not requires_api and not slow and not e2e"
  2. Tests with API keys: Uses free model fallback wrapper that tries multiple free OpenRouter models

The CI workflow automatically discovers tests by marker - no hardcoded test paths.

pytest_changed.py

This script runs only tests affected by your changes:

# Compare against origin/main (default)
uv run python scripts/pytest_changed.py

# Only uncommitted changes
uv run python scripts/pytest_changed.py --base HEAD

# Compare against local main branch
uv run python scripts/pytest_changed.py --base main

# Pass pytest arguments after --
uv run python scripts/pytest_changed.py -- -v --tb=short -m "not slow"

How it maps files to tests

Changed file path Test directory
src/boxpwnr/core/* tests/core/
src/boxpwnr/platforms/* tests/platforms/
src/boxpwnr/executors/* tests/executors/
src/boxpwnr/strategies/* tests/strategies/
src/boxpwnr/tools/* tests/tools/
src/boxpwnr/utils/* tests/reporting/
tests/* The test file itself

Pre-commit Hook

The pre-commit hook automatically runs pytest_changed.py before each commit. It:

  • Warns if tests fail but doesn't block the commit
  • Skip with: git commit --no-verify

Writing New Tests

  1. Place tests in the correct directory based on what you're testing
  2. Add pytestmark = pytest.mark.requires_api if test makes LLM API calls
  3. Use fixtures from conftest.py for common setup (free models, temp directories)
  4. Support --free-models-only flag for API tests to work in CI

Example test with API marker

import pytest

# Mark all tests as requiring API
pytestmark = pytest.mark.requires_api

# Import free models for CI compatibility
from conftest import FREE_MODELS_FALLBACK

class TestMyFeature:
    @pytest.fixture
    def model(self, request):
        """Use free model if --free-models-only flag is set."""
        if request.config.getoption("--free-models-only"):
            return FREE_MODELS_FALLBACK[0]
        return FREE_MODELS_FALLBACK[0]

    def test_something(self, model):
        # Your test using the model
        pass

Common Issues

Test tries to prompt for API key during collection

If you see OSError: pytest: reading from stdin while output is captured, the test is calling SecretManager.get_secret() at module load time. Move such calls inside test functions or fixtures.

Tests not discovered

  • Check file is named test_*.py
  • Check functions are named test_*
  • Check for import errors: uv run pytest --collect-only

Useful Commands

# See what tests would run without running them
uv run pytest --collect-only

# Run specific test file
uv run pytest tests/core/test_chat_completion_message_initialization.py

# Run specific test function
uv run pytest tests/core/test_flag_parsing.py::test_flag_outside_command_block

# Verbose output with short tracebacks
uv run pytest -v --tb=short

# Stop on first failure
uv run pytest -x

Xet Storage Details

Size:
5.17 kB
·
Xet hash:
7e35bba3ad2587926a73b5aaa0406fcc0a1745d99d868cb5b5a1c99f67c79cdd

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.