AgentSkillsCN

acc-detect-test-smells

检测 PHP 测试套件中的测试反模式与代码异味。识别 15 种代码异味(如“测试中的逻辑”、“Mock 过度使用”、“脆弱的测试”、“神秘的客体”等),并提供修复建议与提升测试性的重构模式。

SKILL.md
--- frontmatter
name: acc-detect-test-smells
description: Detects test antipatterns and code smells in PHP test suites. Identifies 15 smells (Logic in Test, Mock Overuse, Fragile Tests, Mystery Guest, etc.) with fix recommendations and refactoring patterns for testability.

Test Smell Detection

Identifies antipatterns and code smells in PHP test suites.

15 Test Smells

#SmellSeverityDetection Pattern
1Logic in TestHighif/for/while/foreach in tests
2Mock OveruseHigh>3 mocks in single test
3Test InterdependenceHighstatic $, @depends
4Fragile TestHighexpects(exactly), at()
5Mystery GuestMediumfile_get_contents, getenv
6Eager TestMediumMultiple unrelated assertions
7Assertion RouletteMedium>5 assertions without context
8Obscure TestLowtest_it_works, test_foo
9Test Code DuplicationMediumRepeated setup/assertion
10Conditional Test LogicMediumif.*assert patterns
11Hard-Coded Test DataLowMagic values in tests
12Testing Private MethodsHighsetAccessible(true)
13Slow TestMediumsleep, I/O in unit tests
14Mocking Final ClassesHighMock concrete final classes
15Mocking Value ObjectsHighMock readonly/immutable classes

Quick Detection Commands

bash
# Logic in tests
Grep: "if \(|for \(|while \(|foreach \(" --glob "tests/**/*Test.php"

# Mock overuse (manual count needed)
Grep: "createMock|createStub" --glob "tests/**/*Test.php"

# Test interdependence
Grep: "static \$|@depends" --glob "tests/**/*Test.php"

# Testing private methods
Grep: "setAccessible\(true\)|ReflectionMethod" --glob "tests/**/*Test.php"

# Mystery guest
Grep: "file_get_contents|fopen|getenv|_ENV|_SERVER" --glob "tests/**/*Test.php"

# Fragile tests
Grep: "expects\(.*exactly|expects\(.*at\(" --glob "tests/**/*Test.php"

Output Format

markdown
# Test Smell Report

## Summary

| Smell | Count | Severity |
|-------|-------|----------|
| Logic in Test | 5 | High |
| Mock Overuse | 3 | High |

## Findings

### Logic in Test (5 occurrences)

| File | Line | Code |
|------|------|------|
| OrderTest.php | 45 | `foreach ($items as $item)` |

**Recommendation:** Extract to data providers or inline values.

## Action Items

1. **High Priority** — Refactor tests with >5 mocks
2. **Medium Priority** — Inline fixture data

Severity Matrix

SeveritySmellsImpact
HighLogic in Test, Mock Overuse, Test Interdependence, Fragile Test, Mocking Final/VO, Testing PrivateUnreliable results, design problems
MediumMystery Guest, Eager Test, Slow Test, Conditional Logic, DuplicationHard to understand/maintain
LowObscure Test, Hard-Coded DataDocumentation/readability

Related Skills

SmellFix With
Mock Overuseacc-create-mock-repository
Mystery Guestacc-create-test-builder
Test Duplicationacc-create-test-builder

References

  • references/smell-catalog.md — Full smell descriptions with code examples
  • references/refactoring-patterns.md — Refactoring patterns for testability