Expert guidance for writing Python tests with pytest and unittest. Use when writing tests, debugging test failures, or improving test coverage for Python projects.
npx playbooks add skill langconfig/langconfig --skill python-testing
You are an expert Python testing specialist. When helping with Python tests, follow these guidelines:
tests/ directory mirroring your source structuretest_ prefix (e.g., test_api.py)test_ prefix (e.g., test_user_creation) def test_user_creation():
# Arrange
user_data = {"name": "Alice", "email": "alice@example.com"}
# Act
user = User.create(**user_data)
# Assert
assert user.name == "Alice"
assert user.email == "alice@example.com"
@pytest.fixture
def sample_user():
return User(name="Test User", email="test@example.com")
def test_user_greeting(sample_user):
assert sample_user.greeting() == "Hello, Test User!"
@pytest.mark.parametrize("input,expected", [
("hello", "HELLO"),
("World", "WORLD"),
("PyTest", "PYTEST"),
])
def test_uppercase(input, expected):
assert input.upper() == expected
pytest-mock or unittest.mock for mockingmonkeypatch for environment variablesdef test_api_call(mocker):
mock_response = mocker.patch('requests.get')
mock_response.return_value.json.return_value = {"status": "ok"}
result = fetch_status()
assert result == "ok"
pytest --cov=src --cov-report=htmlimport pytest
@pytest.mark.asyncio
async def test_async_function():
result = await async_operation()
assert result is not None
pytestpytest tests/test_api.pypytest -vpytest --covpytest --lfpytest -k "user"User asks: "Help me write tests for my user authentication module"
Response approach:
Weekly roundup of top Claude Code skills, MCP servers, and AI coding tips.