Chuyển đến nội dung
Skillpedia
Kiểm thử8.2k · #12542

testing-patterns

Skill này làm gì

Skill này cho AI của bạn các nguyên tắc vững chắc để viết test tự động. Nó bao gồm unit, integration và end-to-end test, cùng lúc nào nên dùng mock. Dành cho lập trình viên muốn một bộ test đáng tin cậy.

Khi nào nên dùng

  • ✓Khi bạn cần viết test cho logic nghiệp vụ mới hoặc các trường hợp biên.
  • ✓Khi test của bạn chậm, không ổn định hoặc phụ thuộc vào dịch vụ bên ngoài.
  • ✓Khi bạn đang quyết định nên viết loại test nào cho một tính năng.

Liên kết skill

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

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/vudovn/ag-kit/blob/main/.agents/skills/testing-patterns/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Viết unit test cho hàm định giá này, bao gồm các trường hợp biên và xử lý lỗi.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Skill giải thích kim tự tháp test và mẫu arrange, act, assert. AI dùng những ý tưởng này để chọn loại test và cấu trúc từng test.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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.

Câu hỏi thường gặp

Kim tự tháp test là gì?

Đó là một hướng dẫn rằng phần lớn test nên là unit test nhanh, với ít integration và end-to-end test hơn ở phía trên.

Những gì không nên viết unit test?

Skill nói không nên test mã của framework, thư viện bên thứ ba hoặc các getter đơn giản.

Một unit test tốt có đặc điểm gì?

Nó nên nhanh, độc lập, lặp lại được, tự kiểm tra kết quả và được viết cùng với mã.

Chi tiết

Mô tả gốc của tác giả

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

repo: vudovn/ag-kit

Độ phổ biến

Sao GitHub
8.2k
Vị trí xếp hạng
#12542
Giấy phép
MIT
Cập nhật
18/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự