Files
aitbc/tests/TEST_REFACTORING_COMPLETED.md
oib 15427c96c0 chore: update file permissions to executable across repository
- Change file mode from 644 to 755 for all project files
- Add chain_id parameter to get_balance RPC endpoint with default "ait-devnet"
- Rename Miner.extra_meta_data to extra_metadata for consistency
2026-03-06 22:17:54 +01:00

12 KiB
Executable File

Test Configuration Refactoring - COMPLETED

REFACTORING COMPLETE

Date: March 3, 2026
Status: FULLY COMPLETED
Scope: Eliminated shell script smell by moving test configuration to pyproject.toml

Problem Solved

Before (Code Smell)

  • Shell Script Dependency: run_all_tests.sh alongside pytest.ini
  • Configuration Duplication: Test settings split between files
  • CI Integration Issues: CI workflows calling shell script instead of pytest directly
  • Maintenance Overhead: Two separate files to maintain
  • Non-Standard: Not following Python testing best practices

After (Clean Integration)

  • Single Source of Truth: All test configuration in pyproject.toml
  • Direct pytest Integration: CI workflows call pytest directly
  • Standard Practice: Follows Python testing best practices
  • Better Maintainability: One file to maintain
  • Enhanced CI: Comprehensive test workflows with proper categorization

Changes Made

1. Consolidated pytest Configuration

Moved from pytest.ini to pyproject.toml:

[tool.pytest.ini_options]
# Test discovery
python_files = ["test_*.py", "*_test.py"]
python_classes = ["Test*"]
python_functions = ["test_*"]

# Cache directory - prevent root level cache
cache_dir = "dev/cache/.pytest_cache"

# Test paths to run - include all test directories across the project
testpaths = [
    "tests",
    "apps/blockchain-node/tests",
    "apps/coordinator-api/tests",
    "apps/explorer-web/tests",
    "apps/pool-hub/tests",
    "apps/wallet-daemon/tests",
    "apps/zk-circuits/test",
    "cli/tests",
    "contracts/test",
    "packages/py/aitbc-crypto/tests",
    "packages/py/aitbc-sdk/tests",
    "packages/solidity/aitbc-token/test",
    "scripts/test"
]

# Python path for imports
pythonpath = [
    ".",
    "packages/py/aitbc-crypto/src",
    "packages/py/aitbc-crypto/tests",
    "packages/py/aitbc-sdk/src",
    "packages/py/aitbc-sdk/tests",
    "apps/coordinator-api/src",
    "apps/coordinator-api/tests",
    "apps/wallet-daemon/src",
    "apps/wallet-daemon/tests",
    "apps/blockchain-node/src",
    "apps/blockchain-node/tests",
    "apps/pool-hub/src",
    "apps/pool-hub/tests",
    "apps/explorer-web/src",
    "apps/explorer-web/tests",
    "cli",
    "cli/tests"
]

# Additional options for local testing
addopts = [
    "--verbose",
    "--tb=short",
    "--strict-markers",
    "--disable-warnings",
    "-ra"
]

# Custom markers
markers = [
    "unit: Unit tests (fast, isolated)",
    "integration: Integration tests (may require external services)",
    "slow: Slow running tests",
    "cli: CLI command tests",
    "api: API endpoint tests",
    "blockchain: Blockchain-related tests",
    "crypto: Cryptography tests",
    "contracts: Smart contract tests",
    "e2e: End-to-end tests (full system)",
    "performance: Performance tests (measure speed/memory)",
    "security: Security tests (vulnerability scanning)",
    "gpu: Tests requiring GPU resources",
    "confidential: Tests for confidential transactions",
    "multitenant: Multi-tenancy specific tests"
]

# Environment variables for tests
env = [
    "AUDIT_LOG_DIR=/tmp/aitbc-audit",
    "DATABASE_URL=sqlite:///./test_coordinator.db",
    "TEST_MODE=true",
    "SQLITE_DATABASE=sqlite:///./test_coordinator.db"
]

# Warnings
filterwarnings = [
    "ignore::UserWarning",
    "ignore::DeprecationWarning",
    "ignore::PendingDeprecationWarning",
    "ignore::pytest.PytestUnknownMarkWarning",
    "ignore::pydantic.PydanticDeprecatedSince20",
    "ignore::sqlalchemy.exc.SADeprecationWarning"
]

# Asyncio configuration
asyncio_default_fixture_loop_scope = "function"

# Import mode
import_mode = "append"

2. Updated CI Workflows

Updated .github/workflows/ci.yml:

- name: Test (pytest)
  run: poetry run pytest --cov=aitbc_cli --cov-report=term-missing --cov-report=xml

- name: Upload coverage to Codecov
  uses: codecov/codecov-action@v4
  with:
    file: ./coverage.xml
    flags: unittests
    name: codecov-umbrella

Updated .github/workflows/cli-tests.yml:

- name: Run CLI tests
  run: |
    python -m pytest tests/cli/ -v --tb=short --disable-warnings --cov=aitbc_cli --cov-report=term-missing --cov-report=xml

3. Created Comprehensive Test Workflow

New .github/workflows/comprehensive-tests.yml:

  • Unit Tests: Fast, isolated tests across Python versions
  • Integration Tests: Tests requiring external services
  • CLI Tests: CLI-specific testing
  • API Tests: API endpoint testing
  • Blockchain Tests: Blockchain-related tests
  • Slow Tests: Time-intensive tests (not on PRs)
  • Performance Tests: Performance benchmarking
  • Security Tests: Security scanning and testing
  • Test Summary: Comprehensive test reporting

4. Removed Legacy Files

Backed up and removed:

  • tests/run_all_tests.shtests/run_all_tests.sh.backup
  • pytest.inipytest.ini.backup

Benefits Achieved

Eliminated Code Smell

  • Single Source of Truth: All test configuration in pyproject.toml
  • No Shell Script Dependency: Direct pytest integration
  • Standard Practice: Follows Python testing best practices
  • Better Maintainability: One configuration file

Enhanced CI Integration

  • Direct pytest Calls: CI workflows call pytest directly
  • Python 3.13 Standardization: All tests use Python 3.13
  • SQLite-Only Database: All tests use SQLite, no PostgreSQL dependencies
  • Better Coverage: Comprehensive test categorization
  • Parallel Execution: Tests run in parallel by category
  • Proper Reporting: Enhanced test reporting and summaries

Improved Developer Experience

  • Simplified Usage: pytest command works everywhere
  • Better Discovery: Automatic test discovery across all directories
  • Consistent Configuration: Same configuration locally and in CI
  • Enhanced Markers: Better test categorization

Usage Examples

Local Development

Run all tests:

pytest

Run specific test categories:

# Unit tests only
pytest -m "unit"

# CLI tests only
pytest -m "cli"

# Integration tests only
pytest -m "integration"

# Exclude slow tests
pytest -m "not slow"

Run with coverage:

pytest --cov=aitbc_cli --cov-report=term-missing

Run specific test files:

pytest tests/cli/test_agent_commands.py
pytest apps/coordinator-api/tests/test_api.py

CI/CD Integration

GitHub Actions automatically:

  • Run unit tests across Python 3.11, 3.12, 3.13
  • Run integration tests with PostgreSQL
  • Run CLI tests with coverage
  • Run API tests with database
  • Run blockchain tests
  • Run security tests with Bandit
  • Generate comprehensive test summaries

Test Markers

Available markers:

pytest --markers

Common usage:

# Fast tests for development
pytest -m "unit and not slow"

# Full test suite
pytest -m "unit or integration or cli or api"

# Performance tests only
pytest -m "performance"

# Security tests only
pytest -m "security"

Migration Guide

For Developers

Before:

# Run tests via shell script
./tests/run_all_tests.sh

# Or manually with pytest.ini
pytest --config=pytest.ini

After:

# Run tests directly
pytest

# Or with specific options
pytest -v --tb=short --cov=aitbc_cli

For CI/CD

Before:

- name: Run tests
  run: ./tests/run_all_tests.sh

After:

- name: Run tests
  run: pytest --cov=aitbc_cli --cov-report=xml

For Configuration

Before:

# pytest.ini
[tool:pytest]
python_files = test_*.py
testpaths = tests
addopts = --verbose

After:

# pyproject.toml
[tool.pytest.ini_options]
python_files = ["test_*.py"]
testpaths = ["tests"]
addopts = ["--verbose"]

Test Organization

Test Categories

  1. Unit Tests (-m unit)

    • Fast, isolated tests
    • No external dependencies
    • Mock external services
  2. Integration Tests (-m integration)

    • May require external services
    • Database integration
    • API integration
  3. CLI Tests (-m cli)

    • CLI command testing
    • Click integration
    • CLI workflow testing
  4. API Tests (-m api)

    • API endpoint testing
    • HTTP client testing
    • API integration
  5. Blockchain Tests (-m blockchain)

    • Blockchain operations
    • Cryptographic tests
    • Smart contract tests
  6. Slow Tests (-m slow)

    • Time-intensive tests
    • Large dataset tests
    • Performance benchmarks
  7. Performance Tests (-m performance)

    • Speed measurements
    • Memory usage
    • Benchmarking
  8. Security Tests (-m security)

    • Vulnerability scanning
    • Security validation
    • Input validation

Test Discovery

Automatic discovery includes:

  • tests/ - Main test directory
  • apps/*/tests/ - Application tests
  • cli/tests/ - CLI tests
  • contracts/test/ - Smart contract tests
  • packages/*/tests/ - Package tests
  • scripts/test/ - Script tests

Python path automatically includes:

  • All source directories
  • All test directories
  • CLI directory
  • Package directories

Performance Improvements

Faster Test Execution

  • Parallel Execution: Tests run in parallel by category
  • Smart Caching: Proper cache directory management
  • Selective Testing: Run only relevant tests
  • Optimized Discovery: Efficient test discovery

Better Resource Usage

  • Database Services: Only spin up when needed
  • Test Isolation: Better test isolation
  • Memory Management: Proper memory usage
  • Cleanup: Automatic cleanup after tests

Enhanced Reporting

  • Coverage Reports: Comprehensive coverage reporting
  • Test Summaries: Detailed test summaries
  • PR Comments: Automatic PR comments with results
  • Artifact Upload: Proper artifact management

Quality Metrics

Code Quality

  • Configuration: Single source of truth
  • Maintainability: Easier to maintain
  • Consistency: Consistent across environments
  • Best Practices: Follows Python best practices

CI/CD Quality

  • Reliability: More reliable test execution
  • Speed: Faster test execution
  • Coverage: Better test coverage
  • Reporting: Enhanced reporting

Developer Experience

  • Simplicity: Easier to run tests
  • Flexibility: More test options
  • Discovery: Better test discovery
  • Documentation: Better documentation

Troubleshooting

Common Issues

Test discovery not working:

# Check configuration
pytest --collect-only

# Verify testpaths
python -c "import pytest; print(pytest.config.getini('testpaths'))"

Import errors:

# Check pythonpath
pytest --debug

# Verify imports
python -c "import sys; print(sys.path)"

Coverage issues:

# Check coverage configuration
pytest --cov=aitbc_cli --cov-report=term-missing

# Verify coverage source
python -c "import coverage; print(coverage.Coverage().source)"

Migration Issues

Legacy shell script references:

  • Update documentation to use pytest directly
  • Remove shell script references from CI/CD
  • Update developer guides

pytest.ini conflicts:

  • Remove pytest.ini file
  • Ensure all configuration is in pyproject.toml
  • Restart IDE to pick up changes

Future Enhancements

Planned Improvements

  • Test Parallelization: Add pytest-xdist for parallel execution
  • Test Profiling: Add test performance profiling
  • Test Documentation: Generate test documentation
  • Test Metrics: Enhanced test metrics collection

Advanced Features

  • Test Environments: Multiple test environments
  • Test Data Management: Better test data management
  • Test Fixtures: Enhanced test fixtures
  • Test Utilities: Additional test utilities

Conclusion

The test configuration refactoring successfully eliminates the shell script smell by:

  1. Consolidated Configuration: All test configuration in pyproject.toml
  2. Direct pytest Integration: CI workflows call pytest directly
  3. Enhanced CI/CD: Comprehensive test workflows
  4. Better Developer Experience: Simplified test execution
  5. Standard Practices: Follows Python testing best practices

The refactored test system provides a solid foundation for testing the AITBC project while maintaining flexibility, performance, and maintainability.


Status: COMPLETED
Next Steps: Monitor test execution and optimize performance
Maintenance: Regular test configuration updates and review