| name | run-unit-tests |
| description | Executes all unit tests and fixes any issues related to the execution results. Use when running tests, identifying root causes of failures, and fixing them. |
// turbo-all
Run Unit Tests
This skill provides a comprehensive guide for executing unit tests in the QuAIA™ framework, identifying root causes of failures, and fixing issues.
Overview
The QuAIA test suite uses:
- pytest as the test framework
- pytest-asyncio for async test support
- unittest.mock for mocking dependencies
Tests are located in the tests/ directory:
tests/
├── conftest.py # Shared fixtures and test setup
├── agents/ # Agent-specific tests
├── orchestrator/ # Orchestrator logic tests
├── common/ # Common utilities tests
└── scripts/ # Script tests
⚡ Auto-Run Policy
This skill is designed to run with minimal interruption.
- Always set
SafeToAutoRun: true for all run_command calls.
- Exceptions: Only set
SafeToAutoRun: false if a step specifically instructs you to "Ask" the user, "Wait" for approval, or "Verify" a destructive action before proceeding.
- Restricted Symbols: Never use the redirection operator (
>) or 2> in commands. Use alternatives (e.g., Set-Content, Out-File, or ignoring errors explicitly).
Step-by-Step Instructions
Step 1: Run All Unit Tests
Execute the full test suite to identify any failures:
pytest -v
pytest -v --tb=long
pytest --cov=. --cov-report=html --cov-report=term-missing
Step 2: Analyze Test Results
After running tests, analyze the output:
- Identify failed tests: Look for
FAILED markers in the output
- Review error messages: Read the assertion errors and tracebacks
- Check test file and line numbers: Note the exact location of failures
Understanding Test Output
FAILED tests/agents/test_example.py::TestClass::test_method - AssertionError: ...
This tells you:
- File:
tests/agents/test_example.py
- Class:
TestClass
- Method:
test_method
- Error type:
AssertionError
Step 3: Identify Root Cause
For each failing test, investigate the root cause:
3.1 Run the Specific Failing Test
pytest tests/agents/test_example.py -v
pytest tests/agents/test_example.py::TestClassName -v
pytest tests/agents/test_example.py::TestClassName::test_method_name -v
pytest tests/agents/test_example.py::TestClassName::test_method_name -v -s --tb=long
3.2 Review the Source Code
- Open the failing test file and locate the test method
- Identify the code under test (the actual implementation being tested)
- Compare expected behavior vs actual behavior
3.3 Check for Common Issues
- Import errors: Module not found or circular imports
- Configuration issues: Missing environment variables or config values
- Mocking problems: Incorrect mock setup or missing patches
- Async issues: Event loop problems or missing
@pytest.mark.asyncio
- Fixture issues: Missing or incorrectly scoped fixtures
Step 4: Fix the Issue
Based on the root cause analysis:
4.1 If the Test is Incorrect
Update the test to match the expected behavior of the implementation:
- Fix incorrect assertions
- Update mock configurations
- Add missing fixtures
4.2 If the Implementation is Incorrect
Fix the source code to match the expected behavior:
- Correct logic errors
- Fix type issues
- Handle edge cases properly
4.3 If Dependencies Changed
Update tests or implementation to accommodate changes:
- Update mock return values
- Adjust expected outputs
- Sync interface changes
Step 5: Re-run Tests and Verify
After making fixes, verify the changes:
pytest tests/path/to/test_file.py::TestClass::test_method -v
pytest tests/path/to/test_file.py -v
pytest -v
pytest --cov=. --cov-report=term-missing
Step 6: Iterate Until All Tests Pass
Repeat Steps 3-5 for each failing test until the entire test suite passes.
Running Tests
Running All Tests
pytest
pytest -v
pytest --cov=. --cov-report=html
Running Specific Tests
pytest tests/agents/
pytest tests/agents/test_requirements_review.py
pytest tests/orchestrator/test_parsing_logic.py::TestGetModelFromArtifacts::test_parse_valid_model
pytest -k "test_agent"
Running Async Tests
pytest tests/orchestrator/test_orchestrator_logic.py -v
Running Tests with Debug Options
pytest -v -s
pytest -x
pytest --lf
pytest --ff
pytest --tb=long -l
Common Issues and Solutions
AsyncIO Event Loop Issues
Symptom: "There is no current event loop" errors
Solution: Mock async components to prevent event loop issues:
@pytest.fixture
def mock_error_history():
"""Mock async components to prevent event loop issues."""
with patch("orchestrator.main.error_history") as mock:
mock.add = AsyncMock()
yield mock
Module Import Issues
Symptom: Imports fail during test collection
Solution: Mock heavy dependencies in conftest.py:
mock_sentence_transformers = MagicMock()
sys.modules["sentence_transformers"] = mock_sentence_transformers
Configuration Issues
Symptom: Tests fail due to missing or incorrect config values
Solution: Always mock config values in fixtures rather than modifying global state:
@pytest.fixture
def mock_config(monkeypatch):
monkeypatch.setattr(config.SomeConfig, "VALUE", "test_value")
Missing Fixtures
Symptom: fixture 'fixture_name' not found
Solution:
- Check if the fixture is defined in
conftest.py
- Ensure the fixture is in scope (same directory or parent)
- Verify the fixture name spelling
Assertion Errors
Symptom: AssertionError: assert X == Y
Solution:
- Compare expected vs actual values carefully
- Check if the implementation changed
- Verify mock return values match expectations
- Use
pytest.approx() for floating-point comparisons
Timeout Issues
Symptom: Tests hang or timeout
Solution:
- Check for infinite loops in the code
- Ensure async functions are properly awaited
- Add timeouts to async operations:
import asyncio
@pytest.mark.asyncio
async def test_with_timeout():
result = await asyncio.wait_for(async_function(), timeout=5.0)
assert result is not None
Mock Not Applied
Symptom: Real functions are called instead of mocks
Solution:
- Verify the patch path is correct (patch where it's used, not where it's defined)
- Ensure patches are applied before the code runs:
with patch("module_under_test.function_name") as mock:
...
with patch("original_module.function_name") as mock:
...
Database/External Service Errors
Symptom: Tests fail due to external service unavailability
Solution:
- Ensure all external services are mocked
- Use fixtures to mock database connections
- Verify environment variables are set for test mode
Verification Checklist
After fixing tests, verify:
Troubleshooting Workflow
┌─────────────────────────────────────────────────┐
│ Run All Tests │
│ pytest -v │
└──────────────────────┬──────────────────────────┘
│
▼
┌────────────────┐
│ Tests Pass? │
└───────┬────────┘
│
┌────────────┴────────────┐
│ │
YES NO
│ │
▼ ▼
┌───────────┐ ┌─────────────────────┐
│ DONE │ │ Identify Failing │
└───────────┘ │ Tests │
└──────────┬──────────┘
│
▼
┌─────────────────────┐
│ Run Specific Test │
│ with Debug Options │
└──────────┬──────────┘
│
▼
┌─────────────────────┐
│ Analyze Traceback │
│ and Error Message │
└──────────┬──────────┘
│
▼
┌─────────────────────┐
│ Identify Root Cause │
│ (Test vs Impl) │
└──────────┬──────────┘
│
▼
┌─────────────────────┐
│ Apply Fix │
└──────────┬──────────┘
│
▼
┌─────────────────────┐
│ Re-run Test │
└──────────┬──────────┘
│
▼
(Loop back to
"Tests Pass?")
Best Practices
- Fix one test at a time: Focus on a single failing test before moving to the next
- Understand before fixing: Ensure you understand why a test is failing before attempting a fix
- Minimal changes: Make the smallest change necessary to fix the issue
- Don't skip tests: Avoid using
@pytest.mark.skip unless absolutely necessary
- Run full suite: Always run the complete test suite after fixes to catch regressions
- Keep tests isolated: Each test should be independent and not rely on other tests
- Document fixes: If a fix reveals a subtle issue, add a comment explaining it