Przejdź do głównej zawartości

Test-Driven Development with AI

Ta treść nie jest jeszcze dostępna w Twoim języku.

Test-Driven Development (TDD) is a powerful methodology for producing reliable software. By writing tests before the implementation, you ensure that your code is correct by design. When you combine TDD with an AI coding assistant, the process becomes even more powerful and efficient.

The AI is exceptionally good at the core TDD loop: generating tests, writing code to pass those tests, and iterating until the requirements are met. Your role shifts from writing boilerplate to defining behavior and verifying the results.

The classic TDD cycle is “Red, Green, Refactor.” With an AI assistant, it looks like this:

1. Write Tests (Red)

You instruct the AI to write a comprehensive suite of tests for a new feature based on its requirements. You explicitly tell it not to write the implementation code yet. This is the “Red” phase—the tests should fail because the code doesn’t exist.

2. Confirm Failure (Red)

You then instruct the AI to run the newly created tests. It will confirm that they fail, verifying that your tests are correctly targeting the unimplemented functionality. This is a crucial sanity check.

3. Implement to Pass (Green)

Now, you give the AI a new, simple instruction: “Write the code that makes these tests pass.” The AI’s goal is no longer ambiguous; it has a clear, verifiable target to aim for.

4. Iterate and Refactor (Green -> Refactor)

The AI will write the implementation, run the tests, and if they fail, it will analyze the errors and refine the code automatically. This loop continues until all tests pass. Once green, you can ask the AI to refactor the code for clarity or performance, knowing the tests will ensure correctness.


Here’s how you can apply this workflow to build a new function.

  1. Prompt for Tests. Start with a clear prompt that specifies the requirements and asks only for tests.

    Write a comprehensive suite of Vitest tests for a new TypeScript function `calculateTotalPrice`.
    This function should take an array of items, each with a `price` and `quantity`, and an optional `discountCode` string.
    - It should correctly calculate the total price.
    - It should handle an empty array, returning 0.
    - If the discount code is "SAVE10", it should apply a 10% discount.
    - Write the tests in `src/lib/calculate.test.ts`, but do not write the implementation in `src/lib/calculate.ts` yet.
  2. Run and See Red. Ask the AI to execute the tests.

    Great. Now run the tests and confirm that they fail.

    The AI will run the test command (e.g., npm test) and report back the expected failures, proving that your test harness is working correctly.

  3. Implement the Code. Now give the green light for implementation.

    Perfect. Now, implement the `calculateTotalPrice` function in `src/lib/calculate.ts` to make all the tests in `src/lib/calculate.test.ts` pass. Do not modify the tests.
  4. Watch the AI Work. The AI will now enter its iterative loop. It will write the function, run the tests, see the output, and adjust the code until it succeeds. This is where features like Cursor’s “YOLO mode” shine, as they can automate this test-fix-retest loop without requiring manual intervention for each step.

  5. Review and Refactor. Once the tests are passing, you have code that is functionally correct according to your specification. You can now review the implementation and ask for improvements.

    The tests are passing. Please refactor the `calculateTotalPrice` function to improve readability and add JSDoc comments.

By using TDD with your AI assistant, you shift your focus from the tedious aspects of writing code to the high-level work of defining behavior and ensuring quality. This not only speeds up development but also leads to more robust and reliable code.