ワンクリックで
write-tests
Use this skill to write unit tests for Python code using pytest.
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
Use this skill to write unit tests for Python code using pytest.
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
| name | write-tests |
| description | Use this skill to write unit tests for Python code using pytest. |
Use this skill to write unit tests for Python code. Tests should be thorough, maintainable, and follow pytest best practices.
tests/ directory mirroring the source structuretest_<module>.pytest_<behavior_being_tested>Do NOT use classes to group tests. Use plain functions instead:
# WRONG - Don't use classes
class TestCalculator:
def test_add(self):
...
# CORRECT - Use plain functions
def test_calculator_adds_two_numbers():
...
def test_calculator_handles_negative_numbers():
...
Group related tests by placing them in the same file or using pytest markers.
Every test MUST follow the AAA pattern with clear visual separation:
def test_player_gains_points_when_solving_puzzle():
# Arrange
player = Player(name="Alice")
puzzle = SimplePuzzle(points=100)
# Act
result = player.solve_puzzle(puzzle, solution="correct_answer")
# Assert
assert result is True
assert player.score == 100
Use blank lines to visually separate each section. Add comments (# Arrange, # Act,
# Assert) only when the separation isn't obvious.
Move complex setup logic to fixtures. Inline setup should be minimal (1-3 lines).
import pytest
@pytest.fixture
def player():
"""A basic player with default settings."""
return Player(name="Test Player")
@pytest.fixture
def game_with_rooms(player):
"""A fully initialized game with rooms loaded."""
game = EscapeRoomGame()
game.load_rooms()
game.player = player
return game
@pytest.fixture
def temp_config_file(tmp_path):
"""A temporary config file for testing file operations."""
config = tmp_path / "config.json"
config.write_text('{"difficulty": "normal"}')
return config
Use appropriate scope to balance isolation and performance:
function (default): Fresh instance per testmodule: Shared across tests in a file (for expensive, read-only resources)session: Shared across all tests (use sparingly)@pytest.fixture(scope="module")
def expensive_resource():
"""Shared across all tests in this module."""
return create_expensive_resource()
@pytest.mark.parametrize("input,expected", [
("north", RoomName.BRIDGE),
("south", RoomName.ENGINEERING),
("invalid", None),
])
def test_parse_direction(input, expected):
result = parse_direction(input)
assert result == expected
def test_invalid_room_raises_error():
game = EscapeRoomGame()
with pytest.raises(ValueError, match="Room not found"):
game.goto_room("nonexistent")
Use tmp_path fixture for file operations:
def test_save_game_creates_file(tmp_path):
save_file = tmp_path / "save.json"
game = EscapeRoomGame()
game.save(save_file)
assert save_file.exists()
assert statements# GOOD - Testing behavior
def test_puzzle_marks_as_solved_after_correct_answer():
puzzle = SimplePuzzle()
puzzle.attempt("correct")
assert puzzle.is_solved is True
# BAD - Testing implementation details
def test_puzzle_internal_state():
puzzle = SimplePuzzle()
puzzle.attempt("correct")
assert puzzle._internal_counter == 1 # Don't test private state
Use descriptive names that explain the scenario and expected behavior:
# GOOD
def test_player_cannot_exit_room_with_unsolved_puzzle():
def test_hint_command_shows_next_hint_when_available():
def test_solve_command_accepts_multiword_answers():
# BAD
def test_exit():
def test_hint():
def test_solve():
# Run all tests
pytest
# Run specific file
pytest tests/test_player.py
# Run with coverage
pytest --cov=src
# Run tests matching pattern
pytest -k "puzzle"