with one click
add-tests
Add comprehensive tests for Python code with pytest
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Menu
Add comprehensive tests for Python code with pytest
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Based on SOC occupation classification
Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Claude's capabilities with specialized knowledge, workflows, or tool integrations.
Perform a thorough code review of recent changes
Knowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."
Create a well-formatted git commit following best practices
| name | add-tests |
| description | Add comprehensive tests for Python code with pytest |
Follow this workflow to add well-structured tests to Python code.
read_file to examine the code that needs testingTests should follow this structure:
"""Tests for module_name."""
import pytest
from module_name import function_to_test, ClassName
@pytest.fixture
def sample_data():
"""Fixture providing sample test data."""
return {
'key': 'value',
'number': 42
}
def test_function_basic_case():
"""Test function_to_test with basic input."""
result = function_to_test('input')
assert result == 'expected_output'
def test_function_edge_case():
"""Test function_to_test with edge case."""
result = function_to_test('')
assert result is None
def test_function_raises_error():
"""Test function_to_test raises appropriate error."""
with pytest.raises(ValueError, match="invalid input"):
function_to_test(None)
class TestClassName:
"""Tests for ClassName."""
def test_init(self):
"""Test ClassName initialization."""
obj = ClassName('param')
assert obj.attribute == 'param'
def test_method(self, sample_data):
"""Test ClassName.method with fixture data."""
obj = ClassName('test')
result = obj.method(sample_data)
assert result == 'expected'
For each function/method, write tests for:
Normal Cases:
Edge Cases:
Error Cases:
Example test structure:
def test_calculate_discount_normal():
"""Test discount calculation with normal values."""
assert calculate_discount(100, 0.1) == 90.0
def test_calculate_discount_zero():
"""Test discount with zero percent."""
assert calculate_discount(100, 0) == 100.0
def test_calculate_discount_invalid():
"""Test discount rejects invalid percentage."""
with pytest.raises(ValueError):
calculate_discount(100, 1.5) # >100% discount
Use pytest fixtures and mocks for external dependencies:
from unittest.mock import Mock, patch
@pytest.fixture
def mock_api_client():
"""Mock API client for testing."""
client = Mock()
client.fetch_data.return_value = {'data': 'test'}
return client
def test_function_with_api(mock_api_client):
"""Test function that calls external API."""
result = process_api_data(mock_api_client)
assert result == 'processed test'
mock_api_client.fetch_data.assert_called_once()
@patch('module.requests.get')
def test_http_request(mock_get):
"""Test function making HTTP request."""
mock_get.return_value.json.return_value = {'status': 'ok'}
result = fetch_remote_data()
assert result['status'] == 'ok'
Use @pytest.mark.parametrize for testing multiple inputs:
@pytest.mark.parametrize("input_val,expected", [
("hello", "HELLO"),
("World", "WORLD"),
("", ""),
("123", "123"),
])
def test_uppercase_conversion(input_val, expected):
"""Test string conversion with various inputs."""
assert to_uppercase(input_val) == expected
tests/ directorytest_<module_name>.pywrite_file to create the test file with contentUse run_shell to:
pytest tests/test_<module>.py -vpytest --cov=<module> tests/test_<module>.pydef test_example():
# Arrange: Set up test data
input_data = {'key': 'value'}
# Act: Call the function
result = function(input_data)
# Assert: Check the result
assert result == expected
# Testing exceptions
with pytest.raises(TypeError):
invalid_function_call()
# Testing warnings
with pytest.warns(UserWarning):
deprecated_function()
# Skipping tests conditionally
@pytest.mark.skipif(sys.version_info < (3, 9), reason="requires python3.9")
def test_new_feature():
pass
# Marking tests
@pytest.mark.slow
def test_slow_operation():
pass
# Testing async code
@pytest.mark.asyncio
async def test_async_function():
result = await async_operation()
assert result == 'expected'