跳到正文
Skillpedia
测试8.2k · #12542

testing-patterns

它能做什么

这项技能为你的 AI 提供编写自动化测试的可靠原则。涵盖单元测试、集成测试和端到端测试,以及何时适合使用模拟。适合希望拥有可靠测试套件的开发者。

适用场景

  • ✓当你需要为新的业务逻辑或边界情况编写测试的时候。
  • ✓当你的测试运行缓慢、时好时坏,或依赖外部服务的时候。
  • ✓当你要为某个功能决定该写哪一种测试的时候。

技能链接

https://github.com/vudovn/ag-kit/blob/main/.agents/skills/testing-patterns/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/vudovn/ag-kit/blob/main/.agents/skills/testing-patterns/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请为这个定价函数编写单元测试,覆盖边界情况和错误处理。”

每周 5 个新技能,1 分钟读完。

工作原理

技能解释测试金字塔,以及准备、执行、断言的模式。AI 运用这些思路来选择测试类型,并组织每个测试的结构。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Testing Patterns

Principles for reliable test suites.


1. Testing Pyramid

        /\          E2E (Few)
       /  \         Critical flows
      /----\
     /      \       Integration (Some)
    /--------\      API, DB queries
   /          \
  /------------\    Unit (Many)
                    Functions, classes

2. AAA Pattern

Step Purpose
Arrange Set up test data
Act Execute code under test
Assert Verify outcome

3. Test Type Selection

When to Use Each

Type Best For Speed
Unit Pure functions, logic Fast (<100ms)
Integration API, DB, services Medium
E2E Critical user flows Slow

4. Unit Test Principles

Good Unit Tests

Principle Meaning
Fast < 100ms each
Isolated No external deps
Repeatable Same result always
Self-checking No manual verification
Timely Written with code

What to Unit Test

Test Don't Test
Business logic Framework code
Edge cases Third-party libs
Error handling Simple getters

5. Integration Test Principles

What to Test

Area Focus
API endpoints Request/response
Database Queries, transactions
External services Contracts

Setup/Teardown

Phase Action
Before All Connect resources
Before Each Reset state
After Each Clean up
After All Disconnect

6. Mocking Principles

When to Mock

Mock Don't Mock
External APIs The code under test
Database (unit) Simple dependencies
Time/random Pure functions
Network In-memory stores

Mock Types

Type Use
Stub Return fixed values
Spy Track calls
Mock Set expectations
Fake Simplified implementation

7. Test Organization

Naming

Pattern Example
Should behavior "should return error when..."
When condition "when user not found..."
Given-when-then "given X, when Y, then Z"

Grouping

Level Use
describe Group related tests
it/test Individual case
beforeEach Common setup

8. Test Data

Strategies

Approach Use
Factories Generate test data
Fixtures Predefined datasets
Builders Fluent object creation

Principles

  • Use realistic data
  • Randomize non-essential values (faker)
  • Share common fixtures
  • Keep data minimal

9. Best Practices

Practice Why
One assert per test Clear failure reason
Independent tests No order dependency
Fast tests Run frequently
Descriptive names Self-documenting
Clean up Avoid side effects

10. Anti-Patterns

❌ Don't ✅ Do
Test implementation Test behavior
Duplicate test code Use factories
Complex test setup Simplify or split
Ignore flaky tests Fix root cause
Skip cleanup Reset state

Remember: Tests are documentation. If someone can't understand what the code does from the tests, rewrite them.

常见问题

什么是测试金字塔?

这是一条指导原则:大多数测试应是快速的单元测试,上层的集成测试和端到端测试应更少。

哪些内容不应做单元测试?

技能说明,不要测试框架代码、第三方库或简单的 getter。

什么样的单元测试才算好?

它应该快速、相互独立、可重复、能自行判断结果,并与代码一起编写。

详情

作者的原始描述

Testing patterns and principles. Unit, integration, mocking strategies.

repo: vudovn/ag-kit

热度

GitHub 星标
8.2k
排名
#12542
许可证
MIT
更新时间
2026/10/18

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能