testing-best-practices by asyrafhussin/agent-skills
npx skills add https://github.com/asyrafhussin/agent-skills --skill testing-best-practices单元测试、集成测试和测试驱动开发(TDD)原则,用于构建可靠、可维护的测试套件。编写有效测试的指南,这些测试能提供信心并作为文档。
在以下情况下参考这些指南:
| 优先级 | 类别 | 影响 | 前缀 |
|---|---|---|---|
| 1 | 测试结构 | 关键 | struct- |
| 2 | 测试隔离 | 关键 | iso- |
| 3 | 断言 | 高 | assert- |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 4 | 测试数据 | 高 | data- |
| 5 | 模拟 | 中 | mock- |
| 6 | 覆盖率 | 中 | cov- |
| 7 | 性能 | 低 | perf- |
struct-aaa - 编排、执行、断言模式struct-naming - 描述性的测试名称struct-one-assert - 每个测试一个逻辑断言struct-describe-it - 组织良好的测试套件struct-given-when-then - 在适当情况下使用行为驱动开发风格iso-independent - 测试独立运行iso-no-shared-state - 无共享可变状态iso-deterministic - 每次运行结果相同iso-no-order-dependency - 可以按任意顺序运行iso-cleanup - 测试后进行清理assert-specific - 具体的断言assert-meaningful-messages - 有用的失败信息assert-expected-first - 期望值在前assert-no-magic-numbers - 使用命名常量data-factories - 使用工厂/构建器data-minimal - 最少的测试数据data-realistic - 真实的边界情况data-fixtures - 管理测试夹具mock-only-boundaries - 在边界处模拟mock-verify-interactions - 验证重要的调用mock-minimal - 不要过度模拟mock-realistic - 真实的模拟行为cov-meaningful - 关注有意义的覆盖率cov-edge-cases - 覆盖边界情况cov-unhappy-paths - 测试错误场景cov-not-100-percent - 100%不是目标perf-fast-unit - 单元测试运行要快perf-slow-integration - 集成测试可以慢一些perf-parallel - 并行运行测试有关详细示例,请参阅 rules/ 目录中的规则文件。
it('calculates total with discount', () => {
// Arrange - set up test data
const cart = new ShoppingCart();
cart.addItem({ name: 'Book', price: 20 });
cart.applyDiscount(0.1);
// Act - execute the code under test
const total = cart.getTotal();
// Assert - verify the result
expect(total).toBe(18);
});
// ✅ Describes behavior and scenario
describe('UserService.register', () => {
it('creates user with hashed password', () => {});
it('throws ValidationError when email is invalid', () => {});
it('sends welcome email after successful registration', () => {});
});
// ❌ Vague names
it('test1', () => {});
it('should work', () => {});
// ✅ Each test sets up its own data
beforeEach(() => {
mockRepository = { save: jest.fn(), find: jest.fn() };
service = new OrderService(mockRepository);
});
// ❌ Shared state between tests
let globalOrder: Order; // Tests depend on each other
/\
/ \ E2E Tests (few)
/----\ - Test critical user flows
/ \ - Slow, expensive
/--------\ Integration Tests (some)
/ \ - Test component interactions
/------------\ - Database, API calls
/ \ Unit Tests (many)
/----------------\ - Fast, isolated
- Test single units
审查测试时,输出发现的问题:
file:line - [category] Description of issue
示例:
tests/user.test.ts:15 - [struct] Missing Arrange/Act/Assert separation
tests/order.test.ts:42 - [iso] Test depends on previous test's state
tests/cart.test.ts:28 - [assert] Multiple unrelated assertions in one test
阅读单独的规则文件以获取详细解释:
rules/struct-aaa-pattern.md
rules/iso-independent-tests.md
rules/data-factories.md
此技能基于以下来源的测试最佳实践构建:
这些规则源自行业最佳实践和测试社区标准。有关详细示例和额外背景信息,请参阅 rules/ 目录中的各个规则文件。
每周安装次数
49
代码仓库
GitHub 星标数
11
首次出现
Jan 24, 2026
安全审计
安装于
codex38
gemini-cli36
opencode36
claude-code33
github-copilot32
antigravity31
Unit testing, integration testing, and TDD principles for reliable, maintainable test suites. Guidelines for writing effective tests that provide confidence and documentation.
Reference these guidelines when:
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Test Structure | CRITICAL | struct- |
| 2 | Test Isolation | CRITICAL | iso- |
| 3 | Assertions | HIGH | assert- |
| 4 | Test Data | HIGH | data- |
| 5 | Mocking | MEDIUM | mock- |
| 6 | Coverage | MEDIUM | cov- |
| 7 | Performance | LOW | perf- |
struct-aaa - Arrange, Act, Assert patternstruct-naming - Descriptive test namesstruct-one-assert - One logical assertion per teststruct-describe-it - Organized test suitesstruct-given-when-then - BDD style when appropriateiso-independent - Tests run independentlyiso-no-shared-state - No shared mutable stateiso-deterministic - Same result every runiso-no-order-dependency - Run in any orderiso-cleanup - Clean up after testsassert-specific - Specific assertionsassert-meaningful-messages - Helpful failure messagesassert-expected-first - Expected value firstassert-no-magic-numbers - Use named constantsdata-factories - Use factories/buildersdata-minimal - Minimal test datadata-realistic - Realistic edge casesdata-fixtures - Manage test fixturesmock-only-boundaries - Mock at boundariesmock-verify-interactions - Verify important callsmock-minimal - Don't over-mockmock-realistic - Realistic mock behaviorcov-meaningful - Focus on meaningful coveragecov-edge-cases - Cover edge casescov-unhappy-paths - Test error scenarioscov-not-100-percent - 100% isn't the goalperf-fast-unit - Unit tests run fastperf-slow-integration - Integration tests can be slowerperf-parallel - Run tests in parallelFor detailed examples, see the rule files in the rules/ directory.
it('calculates total with discount', () => {
// Arrange - set up test data
const cart = new ShoppingCart();
cart.addItem({ name: 'Book', price: 20 });
cart.applyDiscount(0.1);
// Act - execute the code under test
const total = cart.getTotal();
// Assert - verify the result
expect(total).toBe(18);
});
// ✅ Describes behavior and scenario
describe('UserService.register', () => {
it('creates user with hashed password', () => {});
it('throws ValidationError when email is invalid', () => {});
it('sends welcome email after successful registration', () => {});
});
// ❌ Vague names
it('test1', () => {});
it('should work', () => {});
// ✅ Each test sets up its own data
beforeEach(() => {
mockRepository = { save: jest.fn(), find: jest.fn() };
service = new OrderService(mockRepository);
});
// ❌ Shared state between tests
let globalOrder: Order; // Tests depend on each other
/\
/ \ E2E Tests (few)
/----\ - Test critical user flows
/ \ - Slow, expensive
/--------\ Integration Tests (some)
/ \ - Test component interactions
/------------\ - Database, API calls
/ \ Unit Tests (many)
/----------------\ - Fast, isolated
- Test single units
When reviewing tests, output findings:
file:line - [category] Description of issue
Example:
tests/user.test.ts:15 - [struct] Missing Arrange/Act/Assert separation
tests/order.test.ts:42 - [iso] Test depends on previous test's state
tests/cart.test.ts:28 - [assert] Multiple unrelated assertions in one test
Read individual rule files for detailed explanations:
rules/struct-aaa-pattern.md
rules/iso-independent-tests.md
rules/data-factories.md
This skill is built on testing best practices from:
These rules are derived from industry best practices and testing community standards. For detailed examples and additional context, refer to the individual rule files in the rules/ directory.
Weekly Installs
49
Repository
GitHub Stars
11
First Seen
Jan 24, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex38
gemini-cli36
opencode36
claude-code33
github-copilot32
antigravity31
测试策略完整指南:单元/集成/E2E测试金字塔与自动化实践
11,200 周安装