การเขียนการทดสอบก่อน
ฝึกเขียนการทดสอบที่ล้มเหลวเพื่อกำหนดพฤติกรรมที่ต้องการ ก่อนนำโค้ดสำหรับใช้งานจริงมาเขียน
การเขียนการทดสอบก่อน เป็นบทเรียน Testing Mastery: JUnit, Mockito & Integration Tests ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Testing Mastery: JUnit, Mockito & Integration Tests และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Testing Mastery: JUnit, Mockito & Integration Tests มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
TDD's 'Red' Phase
Welcome to the core of Test-Driven Development (TDD)! In this lesson, we'll dive into the crucial first step: writing a failing test first.
This is often called the 'Red' phase of the TDD cycle, because when you run your tests, you expect to see a 'red bar' indicating a failure.
Why Test First?
It might feel counter-intuitive to write a test for code that doesn't exist yet, or isn't fully implemented. But this approach offers great benefits:
- Clarity: It forces you to think about what the code should *do* before you think about *how* to do it.
- Proof of Failure: It proves your test works correctly by showing it can fail. If it passes without any code, your test might be faulty!
- Focus: It gives you a clear, immediate goal: make this one test pass.
Tests as Specifications
Think of your failing test as a precise specification or a mini-requirement. It describes a specific behavior your future code must exhibit.
- It defines the input.
- It defines the expected output or outcome.
- It's concrete and executable.
This helps prevent misunderstandings and ensures your code delivers exactly what's needed for that specific scenario.
Scenario: Simple Calculator
Let's practice by building a simple Calculator class. For our first piece of functionality, we want it to be able to add two numbers.
Our goal is to write a test for an add method that will fail before we even write the correct implementation of add.
Designing the Failing Test
Before writing code, let's think:
- What input? We'll add
2and3. - What output? We expect the result to be
5. - What method? A method called
addon aCalculatorobject.
We'll write a test that calls calculator.add(2, 3) and asserts that the result is 5.
Code Demo: The Failing Test
Here's a simulated example of writing a test first. Notice the Calculator's add method is intentionally incorrect (it returns 0). This makes our 'test' fail, demonstrating the 'Red' phase.
Run this code to see the simulated failure!
public class Main {
public static void main(String[] args) {
System.out.println("Running a simulated test...");
Calculator calculator = new Calculator();
// Our test scenario: 2 + 3 should be 5
int expected = 5;
int actual = calculator.add(2, 3);
if (expected == actual) {
System.out.println("Test PASSED: 2 + 3 = " + actual);
} else {
System.out.println("Test FAILED: Expected " + expected + " but got " + actual);
System.out.println("This is our 'Red' phase!");
}
}
}
// The production code (intentionally incorrect for 'Red' phase)
class Calculator {
public int add(int a, int b) {
return 0; // Wrong implementation to make the test fail
}
}Analyzing the 'Red'
After running the previous code, you saw a 'Test FAILED' message. This is exactly what we wanted!
- Expected:
5 - Actual:
0
This failure confirms two things: our test is correctly written, and the production code (the add method) does not yet meet the specified behavior.
The 'Minimum Code' Mindset
Once you have a failing test, the next step in TDD (the 'Green' phase) is to write the *simplest possible code* to make that test pass.
- Don't over-engineer.
- Don't add extra features.
- Just focus on satisfying the current failing test.
This keeps your code clean and focused, building functionality incrementally.
Advantages of Test-First
Embracing the 'Red' phase of TDD leads to:
- Better Design: Tests guide you towards more modular and testable code.
- Increased Confidence: Each passing test gives you confidence in your code's correctness.
- Reduced Bugs: Catch issues early, before they become complex problems.
- Living Documentation: Your tests serve as up-to-date examples of how your code should be used.
Quick Check on TDD Red
Let's test your understanding of the 'Red' phase in Test-Driven Development.
Recap: The 'Red' Phase
You've explored the crucial first step of TDD: writing a failing test.
- The 'Red' phase means writing a test that fails.
- This test acts as a clear specification for new functionality.
- Seeing the test fail confirms its validity and that the code isn't yet correct.
- This methodical approach leads to clearer requirements and more robust code.
Next, we'll learn how to make that failing test pass – the 'Green' phase!
คำถามที่พบบ่อย
บทเรียน “การเขียนการทดสอบก่อน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเขียนการทดสอบก่อน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Testing Mastery: JUnit, Mockito & Integration Tests ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Testing Mastery: JUnit, Mockito & Integration Tests มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเขียนการทดสอบก่อน”
ฝึกเขียนการทดสอบที่ล้มเหลวเพื่อกำหนดพฤติกรรมที่ต้องการ ก่อนนำโค้ดสำหรับใช้งานจริงมาเขียน คุณปฏิบัติ Testing Mastery: JUnit, Mockito & Integration Tests ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Testing Mastery: JUnit, Mockito & Integration Tests หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Testing Mastery: JUnit, Mockito & Integration Tests บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การเขียนการทดสอบก่อน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Testing Mastery: JUnit, Mockito & Integration Tests นี้ได้ไหม
ได้ บทเรียน Testing Mastery: JUnit, Mockito & Integration Tests ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- แนะนำวงจร TDD
- การเขียนการทดสอบก่อน
- การปรับโครงสร้างโค้ดเพื่อความสามารถในการทดสอบ
- กฎสามข้อของ TDD