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

test-driven-development

Skill này làm gì

Skill này khiến AI của bạn viết test trước khi viết code cho tính năng mới, bản sửa lỗi và thay đổi hành vi. Skill dành cho lập trình viên muốn code được test bao phủ ngay từ đầu.

Khi nào nên dùng

  • ✓Khi bạn yêu cầu AI thêm một tính năng mới hoặc thay đổi cách code vận hành.
  • ✓Khi bạn yêu cầu AI sửa lỗi và muốn có một bài test chứng minh bản sửa đúng.
  • ✓Khi bạn muốn tái cấu trúc code an toàn với các bài test đã có sẵn.

Liên kết skill

https://github.com/NousResearch/hermes-agent/blob/main/skills/software-development/test-driven-development/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/NousResearch/hermes-agent/blob/main/skills/software-development/test-driven-development/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

“Thêm tính năng thử lại cho hàm tải lên của chúng tôi, và viết test trước để chúng ta biết nó hoạt động.”

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

Cách hoạt động

Skill tuân theo chu trình đỏ, xanh lá, tái cấu trúc: viết một bài test nhỏ và xem nó thất bại, viết đoạn code ít nhất để vượt qua, rồi dọn dẹp. AI xóa mọi code nó đã viết trước khi bài test thất bại.

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.

Test-Driven Development (TDD)

Overview

Write the test first. Watch it fail. Write minimal code to pass.

Core principle: If you didn't watch the test fail, you don't know if it tests the right thing.

Violating the letter of the rules is violating the spirit of the rules.

When to Use

Always:

  • New features
  • Bug fixes
  • Refactoring
  • Behavior changes

Exceptions (ask the user first):

  • Throwaway prototypes
  • Generated code
  • Configuration files

Thinking "skip TDD just this once"? Stop. That's rationalization.

The Iron Law

NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST

Write code before the test? Delete it. Start over.

No exceptions:

  • Don't keep it as "reference"
  • Don't "adapt" it while writing tests
  • Don't look at it
  • Delete means delete

Implement fresh from tests. Period.

Red-Green-Refactor Cycle

RED — Write Failing Test

Write one minimal test showing what should happen.

Good test:

def test_retries_failed_operations_3_times():
    attempts = 0
    def operation():
        nonlocal attempts
        attempts += 1
        if attempts < 3:
            raise Exception('fail')
        return 'success'

    result = retry_operation(operation)

    assert result == 'success'
    assert attempts == 3

Clear name, tests real behavior, one thing.

Bad test:

def test_retry_works():
    mock = MagicMock()
    mock.side_effect = [Exception(), Exception(), 'success']
    result = retry_operation(mock)
    assert result == 'success'  # What about retry count? Timing?

Vague name, tests mock not real code.

Requirements:

  • One behavior per test
  • Clear descriptive name ("and" in name? Split it)
  • Real code, not mocks (unless truly unavoidable)
  • Name describes behavior, not implementation

Verify RED — Watch It Fail

MANDATORY. Never skip.

# Use terminal tool to run the specific test
pytest tests/test_feature.py::test_specific_behavior -v

Confirm:

  • Test fails (not errors from typos)
  • Failure message is expected
  • Fails because the feature is missing

Test passes immediately? You're testing existing behavior. Fix the test.

Test errors? Fix the error, re-run until it fails correctly.

GREEN — Minimal Code

Write the simplest code to pass the test. Nothing more.

Good:

def add(a, b):
    return a + b  # Nothing extra

Bad:

def add(a, b):
    result = a + b
    logging.info(f"Adding {a} + {b} = {result}")  # Extra!
    return result

Don't add features, refactor other code, or "improve" beyond the test.

Cheating is OK in GREEN:

  • Hardcode return values
  • Copy-paste
  • Duplicate code
  • Skip edge cases

We'll fix it in REFACTOR.

Verify GREEN — Watch It Pass

MANDATORY.

# Run the specific test
pytest tests/test_feature.py::test_specific_behavior -v

# Then run ALL tests to check for regressions
pytest tests/ -q

Confirm:

  • Test passes
  • Other tests still pass
  • Output pristine (no errors, warnings)

Test fails? Fix the code, not the test.

Other tests fail? Fix regressions now.

REFACTOR — Clean Up

After green only:

  • Remove duplication
  • Improve names
  • Extract helpers
  • Simplify expressions

Keep tests green throughout. Don't add behavior.

If tests fail during refactor: Undo immediately. Take smaller steps.

Repeat

Next failing test for next behavior. One cycle at a time.

Avoid Horizontal Slices

Do not write all tests first and then all implementation. That is horizontal slicing: RED becomes "write a pile of imagined tests" and GREEN becomes "make the pile pass." It produces brittle tests because the tests are designed before the implementation has taught you what behavior and interface actually matter.

Use vertical tracer bullets instead:

WRONG:
  RED:   test1, test2, test3, test4
  GREEN: impl1, impl2, impl3, impl4

RIGHT:
  RED→GREEN: test1→impl1
  RED→GREEN: test2→impl2
  RED→GREEN: test3→impl3

A tracer bullet is one end-to-end behavior slice. It proves the path works, teaches you about the interface, and keeps each next test grounded in what you just learned.

Why Order Matters

"I'll write tests after to verify it works"

Tests written after code pass immediately. Passing immediately proves nothing:

  • Might test the wrong thing
  • Might test implementation, not behavior
  • Might miss edge cases you forgot
  • You never saw it catch the bug

Test-first forces you to see the test fail,

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

Có ngoại lệ nào không?

Có. Skill cho biết nên hỏi trước với các nguyên mẫu dùng một lần, code được sinh tự động và file cấu hình.

Vì sao bài test phải thất bại trước?

Skill cho biết nếu bạn chưa thấy bài test thất bại, bạn không biết nó có kiểm tra đúng điều không.

Nếu tôi đã viết code rồi thì sao?

Skill cho biết nên xóa code và bắt đầu lại từ các bài test.

Chi tiết

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

TDD: enforce RED-GREEN-REFACTOR, tests before code.

repo: NousResearch/hermes-agent

Độ phổ biến

Sao GitHub
260k
Vị trí xếp hạng
#626
Giấy phép
MIT
Cập nhật
2/11/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ự