# Running Tests Complete guide for running the TradingAgents test suite. ## Prerequisites Install test dependencies: ```bash pip install -r requirements.txt pytest --version # Verify pytest is installed ``` ## Basic Usage ### Run All Tests ```bash pytest tests/ ``` ### Run with Verbose Output ```bash pytest tests/ -v ``` ### Run with Coverage ```bash pytest tests/ --cov=tradingagents --cov-report=html ``` View coverage report: ```bash open htmlcov/index.html # macOS xdg-open htmlcov/index.html # Linux start htmlcov/index.html # Windows ``` ## Test Selection ### By Directory ```bash # Unit tests only pytest tests/unit/ # Integration tests only pytest tests/integration/ # Regression tests only pytest tests/regression/ ``` ### By File ```bash pytest tests/unit/test_analysts.py ``` ### By Test Function ```bash pytest tests/unit/test_analysts.py::test_market_analyst_initialization ``` ### By Test Class ```bash pytest tests/unit/test_analysts.py::TestMarketAnalyst ``` ### By Pattern ```bash # Run all tests matching pattern pytest -k "analyst" # Run tests NOT matching pattern pytest -k "not integration" # Multiple patterns pytest -k "analyst and not integration" ``` ### By Markers ```bash # Smoke tests (critical path) pytest -m smoke # Integration tests pytest -m integration # Skip slow tests pytest -m "not slow" ``` ## Output Options ### Minimal Output ```bash pytest tests/ -q ``` ### Show All Output ```bash pytest tests/ -v -s ``` ### Show Only Failures ```bash pytest tests/ --tb=short ``` ### Show Failed Tests First ```bash pytest tests/ --failed-first ``` ### Stop on First Failure ```bash pytest tests/ -x ``` ### Stop After N Failures ```bash pytest tests/ --maxfail=3 ``` ## Parallel Execution Run tests in parallel for faster execution: ```bash # Install pytest-xdist pip install pytest-xdist # Run with 4 workers pytest tests/ -n 4 # Run with auto-detect workers pytest tests/ -n auto ``` ## Test Coverage ### Generate Coverage Report ```bash pytest tests/ --cov=tradingagents --cov-report=term-missing ``` ### Coverage with HTML Report ```bash pytest tests/ --cov=tradingagents --cov-report=html ``` ### Coverage for Specific Module ```bash pytest tests/ --cov=tradingagents.agents --cov-report=term ``` ### Minimum Coverage Threshold ```bash pytest tests/ --cov=tradingagents --cov-fail-under=80 ``` ## Debugging Tests ### Run with Python Debugger ```bash pytest tests/ --pdb ``` ### Drop into Debugger on Failure ```bash pytest tests/ --pdb -x ``` ### Show Local Variables on Failure ```bash pytest tests/ -l ``` ### Show Print Statements ```bash pytest tests/ -s ``` ## Environment Setup ### Set Environment Variables ```bash # For single test run OPENAI_API_KEY=test_key pytest tests/ # Or create .env.test cat > .env.test <