Langsung ke konten
Skillpedia
Testing9.8k ยท #12208

test-driven-development

Fungsinya

Panduan ini membuat agen Anda menulis tes yang gagal sebelum menulis kode yang memperbaiki atau menambah perilaku. Ditujukan untuk pengembang yang menginginkan bukti bahwa sebuah perubahan berfungsi dan tidak merusak bagian lain.

Kapan dipakai

  • โœ“Saat Anda meminta agen menambahkan logika baru atau mengubah perilaku yang ada.
  • โœ“Saat seseorang melaporkan bug dan Anda ingin bug itu direproduksi dengan tes sebelum diperbaiki.
  • โœ“Saat Anda ingin menambahkan penanganan untuk kasus tepi tanpa merusak perilaku saat ini.

Link skill

https://github.com/cloudflare/computer/blob/main/.agents/skills/test-driven-development/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/cloudflare/computer/blob/main/.agents/skills/test-driven-development/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit โ†’

Contoh permintaan ke AI

โ€œAda bug di mana judul tugas tersimpan dalam keadaan kosong. Tulis tes yang gagal untuknya dulu, lalu perbaiki.โ€

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

Skill mengikuti siklus: tulis tes yang gagal, tulis kode terkecil yang membuatnya lolos, lalu rapikan selagi tes masih lolos. Untuk bug, skill terlebih dahulu mereproduksi bug dengan tes. Skill tidak berlaku untuk perubahan konfigurasi murni atau dokumentasi.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.

Test-Driven Development

Overview

Write a failing test before writing the code that makes it pass. For bug fixes, reproduce the bug with a test before attempting a fix. Tests are proof โ€” "seems right" is not done. A codebase with good tests is an AI agent's superpower; a codebase without tests is a liability.

When to Use

  • Implementing any new logic or behavior
  • Fixing any bug (the Prove-It Pattern)
  • Modifying existing functionality
  • Adding edge case handling
  • Any change that could break existing behavior

When NOT to use: Pure configuration changes, documentation updates, or static content changes that have no behavioral impact.

The TDD Cycle

    RED                GREEN              REFACTOR
 Write a test    Write minimal code    Clean up the
 that fails  โ”€โ”€โ†’  to make it pass  โ”€โ”€โ†’  implementation  โ”€โ”€โ†’  (repeat)
      โ”‚                  โ”‚                    โ”‚
      โ–ผ                  โ–ผ                    โ–ผ
   Test FAILS        Test PASSES         Tests still PASS

Step 1: RED โ€” Write a Failing Test

Write the test first. It must fail. A test that passes immediately proves nothing.

// RED: This test fails because createTask doesn't exist yet
describe('TaskService', () => {
  it('creates a task with title and default status', async () => {
    const task = await taskService.createTask({ title: 'Buy groceries' });

    expect(task.id).toBeDefined();
    expect(task.title).toBe('Buy groceries');
    expect(task.status).toBe('pending');
    expect(task.createdAt).toBeInstanceOf(Date);
  });
});

Step 2: GREEN โ€” Make It Pass

Write the minimum code to make the test pass. Don't over-engineer:

// GREEN: Minimal implementation
export async function createTask(input: { title: string }): Promise<Task> {
  const task = {
    id: generateId(),
    title: input.title,
    status: 'pending' as const,
    createdAt: new Date(),
  };
  await db.tasks.insert(task);
  return task;
}

Step 3: REFACTOR โ€” Clean Up

With tests green, improve the code without changing behavior:

  • Extract shared logic
  • Improve naming
  • Remove duplication
  • Optimize if necessary

Run tests after every refactor step to confirm nothing broke.

The Prove-It Pattern (Bug Fixes)

When a bug is reported, do not start by trying to fix it. Start by writing a test that reproduces it.

Bug report arrives
       โ”‚
       โ–ผ
  Write a test that demonstrates the bug
       โ”‚
       โ–ผ
  Test FAILS (confirming the bug exists)
       โ”‚
       โ–ผ
  Implement the fix
       โ”‚
       โ–ผ
  Test PASSES (proving the fix works)
       โ”‚
       โ–ผ
  Run full test suite (no regressions)

Example:

// Bug: "Completing a task doesn't update the completedAt timestamp"

// Step 1: Write the reproduction test (it should FAIL)
it('sets completedAt when task is completed', async () => {
  const task = await taskService.createTask({ title: 'Test' });
  const completed = await taskService.completeTask(task.id);

  expect(completed.status).toBe('completed');
  expect(completed.completedAt).toBeInstanceOf(Date);  // This fails โ†’ bug confirmed
});

// Step 2: Fix the bug
export async function completeTask(id: string): Promise<Task> {
  return db.tasks.update(id, {
    status: 'completed',
    completedAt: new Date(),  // This was missing
  });
}

// Step 3: Test passes โ†’ bug fixed, regression guarded

The Test Pyramid

Invest testing effort according to the pyramid โ€” most tests should be small and fast, with progressively fewer tests at higher levels:

          โ•ฑโ•ฒ
         โ•ฑ  โ•ฒ         E2E Tests (~5%)
        โ•ฑ    โ•ฒ        Full user flows, real browser
       โ•ฑโ”€โ”€โ”€โ”€โ”€โ”€โ•ฒ
      โ•ฑ        โ•ฒ      Integration Tests (~15%)
     โ•ฑ          โ•ฒ     Component interactions, API boundaries
    โ•ฑโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฒ
   โ•ฑ              โ•ฒ   Unit Tests (~80%)
  โ•ฑ                โ•ฒ  Pure logic, isolated, milliseconds each
 โ•ฑโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฒ

The Beyonce Rule: If you liked it, you should have put a test on it. Infrastructure changes, refactoring, and migrations are not responsible for catching your bugs โ€” your tests are. If a change breaks your code and you didn't have a test for it, that's on you.

Test Sizes (Resource Model)

Beyond the pyramid levels, classify tests by what resources they consume:

Size Constraints Speed Example
Small Single process, no I/O, no network, no database Milliseconds Pure function tests, data tra
Pertanyaan umum

Apakah ini hanya untuk fitur baru?

Tidak. Ini juga berlaku untuk perbaikan bug dan perubahan pada perilaku yang sudah ada.

Mengapa tes harus gagal lebih dulu?

Tes yang langsung lolos tidak membuktikan apa pun, sehingga skill memastikan tes itu gagal sebelum perbaikan.

Kapan saya boleh melewatinya?

Untuk perubahan konfigurasi murni, pembaruan dokumentasi, atau konten statis tanpa perubahan perilaku.

Detail

Deskripsi asli dari pembuat

Drives development with tests. Use when implementing any logic, fixing any bug, or changing any behavior. Use when you need to prove that code works, when a bug report arrives, or when you're about to modify existing functionality.

repo: cloudflare/computer

Popularitas

Bintang GitHub
9.8k
Peringkat
#12208
Lisensi
MIT
Diperbarui
2/11/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • โ€ฆ

Skill lain dari repositori ini

Skill serupa