Advanced Testing Skills
Mocking and Patching
Mocking allows you to replace parts of your system with mock objects to test complex scenarios:
from unittest.mock import patch
def external_api_call():
## Simulated external API call
return "Real API Response"
def test_mocking():
with patch('__main__.external_api_call') as mock_api:
mock_api.return_value = "Mocked Response"
result = external_api_call()
assert result == "Mocked Response"
Test Coverage Analysis
## Install coverage tool
pip3 install coverage
## Run tests with coverage
coverage run -m pytest
coverage report -m
Coverage Metrics
Metric |
Description |
Line Coverage |
Percentage of code lines executed |
Branch Coverage |
Percentage of decision branches tested |
Function Coverage |
Percentage of functions called |
Continuous Integration Testing
graph TD
A[Code Commit] --> B[CI Pipeline]
B --> C{Test Execution}
C --> |Pass| D[Deploy]
C --> |Fail| E[Notify Developer]
Advanced Pytest Techniques
Parametrized Complex Tests
@pytest.mark.parametrize("user_input,expected", [
({"age": 25, "status": "active"}, True),
({"age": 17, "status": "inactive"}, False),
({"age": 30, "status": "pending"}, False)
])
def test_user_validation(user_input, expected):
def validate_user(user):
return user['age'] >= 18 and user['status'] == 'active'
assert validate_user(user_input) == expected
import pytest
import time
def test_performance():
start_time = time.time()
## Function to test
result = complex_calculation()
end_time = time.time()
execution_time = end_time - start_time
assert execution_time < 0.1 ## Must complete in less than 100ms
Handling Asynchronous Code
import asyncio
import pytest
async def async_function():
await asyncio.sleep(1)
return "Completed"
@pytest.mark.asyncio
async def test_async_function():
result = await async_function()
assert result == "Completed"
Error and Exception Testing
def test_exception_handling():
with pytest.raises(ValueError) as excinfo:
def risky_function():
raise ValueError("Custom error")
risky_function()
assert "Custom error" in str(excinfo.value)
Advanced Configuration
Create a pytest.ini
file for custom configurations:
[pytest]
addopts = -v --maxfail=2
testpaths = tests
python_files = test_*.py
Best Practices for Advanced Testing
- Use minimal and focused tests
- Avoid testing implementation details
- Maintain test independence
- Keep tests readable and maintainable
At LabEx, we emphasize the importance of comprehensive and intelligent testing strategies that go beyond basic test coverage.
Debugging Test Failures
## Detailed test output
pytest -v --tb=short
## Stop on first failure
pytest -x
## Print local variables on failure
pytest -l