AgentSkillsCN

unit-test-standards

遵循Python单元测试标准,包括命名规范(test__<目标>__<预期>)、行为测试模式、覆盖率要求(最低80%),以及对冗余测试的自动检测机制。无论是审查测试用例、编写测试代码,还是强化测试质量,此标准都能为您提供有力指导。触发词包括:单元测试、测试命名、测试覆盖率、冗余测试、行为测试。

SKILL.md
--- frontmatter
name: unit-test-standards
description: Python unit test standards including naming conventions (test__<what>__<expected>), behavioral testing patterns, coverage requirements (80% minimum), and tautological test detection. Use when reviewing tests, writing tests, or enforcing test quality. Trigger terms: unit test, test naming, test coverage, tautological test, behavioral test.
allowed-tools: Read, Grep, Glob

Python Unit Test Standards

Standards for writing meaningful, maintainable tests that verify behavior.

Test Naming Convention

python
# Unit tests: test__<what>__<expected>
def test__batch_allocation__reduces_available_quantity():
    pass

# Integration tests: test__<component>__<behavior>
def test__repository__saves_and_retrieves_batch():
    pass

# E2E tests: test__<use_case>__<scenario>
def test__order_allocation__happy_path():
    pass

Quality Criteria

Good (Behavioral)Bad (Tautological)
Test outcomes and state changesAssert mocked return values
Use real objects where possibleTest that assignment works
Verify domain invariantsTests with no assertions
Test error conditions explicitlyTest implementation details

Coverage Requirements

ScopeMinimumTarget
New code80%90%
Critical paths95%100%
Overall project70%80%

File Organization

code
tests/
├── unit/           # Fast, isolated (<100ms each)
├── integration/    # Database, API, external services
├── e2e/           # Full workflow tests
└── conftest.py    # Shared fixtures

Tautological Test Detection

Tests that can't fail provide false confidence:

  1. Mock assertion - Verifying mock returns what you told it to
  2. Assignment test - Testing that x = 5 results in x == 5
  3. Empty test - No assertions or only pass
  4. Implementation test - Testing HOW not WHAT

See reference.md for detection patterns and examples.md for comparisons.