Skill: Debug Test Failures
When to Use This Skill
This skill activates when you need to:
- •Investigate why tests are failing
- •Debug specific test failures
- •Understand test error messages
- •Fix flaky or intermittent test failures
Prerequisites
This assumes tests are configured and running. Use run-tests skill to execute tests first.
Step-by-Step Workflow
Step 1: Run Tests and Identify Failures
Run tests with verbose output:
# Python pytest -v # JavaScript npm test -- --verbose # Go go test -v ./...
Identify the failing test(s):
- •Note the test file and function name
- •Read the error message carefully
- •Check the assertion that failed
Step 2: Run Only the Failing Test
Python (pytest):
pytest tests/test_file.py::test_function_name -v
JavaScript (Jest):
npm test -- tests/example.test.js -t "test name pattern"
Go:
go test -run TestFunctionName -v
Step 3: Add Debug Output
Python - Add print statements:
def test_example():
result = my_function(input_data)
print(f"DEBUG: result = {result}") # Debug output
print(f"DEBUG: type = {type(result)}")
assert result == expected
Run with output visible:
pytest -s tests/test_file.py::test_function_name
Python - Use pytest's debugging:
# Drop into debugger on failure pytest --pdb # Drop into debugger on first failure pytest -x --pdb
JavaScript - Add console.log:
test('example', () => {
const result = myFunction(inputData);
console.log('DEBUG: result =', result);
expect(result).toBe(expected);
});
Step 4: Common Failure Patterns
AssertionError / Unexpected Value
Cause: Expected value doesn't match actual value
Debug steps:
- •Print both expected and actual values
- •Check data types match (string vs int, etc.)
- •Check for whitespace, case sensitivity
- •Verify test data setup
Example fix:
# Before - fails due to extra whitespace assert result == "hello" # After - strip whitespace assert result.strip() == "hello"
Import / Module Not Found
Cause: Module path issues or missing dependencies
Debug steps:
- •Check if module is installed:
pip list | grep module - •Verify PYTHONPATH or NODE_PATH
- •Install in development mode:
pip install -e . - •Check import statement spelling
Example fix:
# Add to tests/conftest.py import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
Timeout Errors
Cause: Test taking too long or blocking operation
Debug steps:
- •Check for infinite loops
- •Verify async/await used correctly
- •Mock external API calls
- •Increase timeout temporarily to see if test passes
Example fix:
# Pytest - increase timeout
@pytest.mark.timeout(300)
def test_slow_operation():
result = slow_function()
assert result is not None
Fixture / Setup Errors
Cause: Test fixtures or setup not working
Debug steps:
- •Check fixture names match exactly
- •Verify fixture scope (function, class, module)
- •Print fixture values in test
- •Check conftest.py is in correct location
Example fix:
# Check fixture is working
def test_example(sample_data):
print(f"Fixture value: {sample_data}") # Debug
assert sample_data is not None
Flaky / Intermittent Failures
Cause: Race conditions, timing issues, or external dependencies
Debug steps:
- •Run test multiple times:
pytest --count=10 - •Check for shared state between tests
- •Verify test isolation (each test is independent)
- •Mock time-dependent or random operations
- •Check for test order dependencies
Example fix:
# Mock random values
from unittest.mock import patch
@patch('random.randint', return_value=5)
def test_with_random(mock_random):
result = function_using_random()
assert result == expected_value
Step 5: Use Debugging Tools
Python Debugger (pdb)
Drop into debugger on failure:
pytest --pdb
Add breakpoint in code:
def test_example():
data = prepare_data()
breakpoint() # Python 3.7+
# or: import pdb; pdb.set_trace()
result = process(data)
assert result == expected
pdb commands:
- •
l- list code - •
n- next line - •
s- step into function - •
c- continue execution - •
p variable- print variable - •
pp variable- pretty print - •
q- quit debugger
JavaScript Debugger
Node.js debugger:
node --inspect-brk node_modules/.bin/jest --runInBand
Add debugger statement:
test('example', () => {
const data = prepareData();
debugger; // Pauses here in debugger
const result = process(data);
expect(result).toBe(expected);
});
VS Code Debugging
Python .vscode/launch.json:
{
"name": "Python: Debug Tests",
"type": "python",
"request": "launch",
"module": "pytest",
"args": ["tests/test_file.py", "-v"]
}
JavaScript .vscode/launch.json:
{
"name": "Jest: Debug",
"type": "node",
"request": "launch",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["--runInBand", "--no-cache"],
"console": "integratedTerminal"
}
Step 6: Fix and Verify
- •Make minimal changes to fix the issue
- •Run the failing test again
- •Run all related tests
- •Run full test suite to ensure no regressions
Common Issues and Solutions
Issue: Test passes locally but fails in CI
Solutions:
- •Check environment variables
- •Verify versions match (Python, Node, etc.)
- •Check for file system case sensitivity
- •Review CI logs for warnings
- •Test with same database/fixtures as CI
Issue: Can't reproduce failure locally
Solutions:
- •Clear caches:
pytest --cache-clearorjest --clearCache - •Run in clean environment (new virtualenv)
- •Check for environment-specific config
- •Run with same seed/random state
- •Check system resources (memory, disk space)
Issue: Error message unclear
Solutions:
- •Add custom error messages to assertions
- •Use pytest's
-vvfor extra verbose output - •Check test framework documentation
- •Add logging before assertions
- •Simplify test to isolate issue
Success Criteria
- •✅ Identified root cause of test failure
- •✅ Fixed the issue with minimal changes
- •✅ Failing test now passes
- •✅ Related tests still pass
- •✅ Understanding why it failed to prevent future issues
Related Skills
- •run-tests - Execute tests
- •pytest-setup - Configure test framework
- •code-formatting - Fix linting/formatting issues