softwaretesting programming go
SEPQM10.1 - Test Driven Development
Test Driven Development
Canon TDD - by Kent Beck - Software Design: Tidy First?
Introduction
Test-Driven Development (TDD) is a technique for building software that guides software development by writing tests. It was developed by Kent Beck in the late 1990’s as part of Extreme Programming. In essence we follow three simple steps repeatedly:
- Write a test for the next bit of functionality you want to add.
- Write the functional code until the test passes.
- Refactor both new and old code to make it well structured.

Although these three steps, often summarized as _Red - Green - Refactor_, are the heart of the process, there’s also a vital initial step where we write out a list of test cases first. We then pick one of these tests, apply red-green-refactor to it, and once we’re done pick the next. Sequencing the tests properly is a skill, we want to pick tests that drive us quickly to the salient points in the design. During the process we should add more tests to our lists as they occur to us.

Simple Explanation
TDD is a way of writing software where you:
- Write a test before writing the actual code.
- Run the test, and it should fail (because the code doesn’t exist yet).
- Write the code that makes the test pass.
- Run the test again – it should now pass.
- Refactor your code if needed, keeping the test green (still passing).
Red → Green → Refactor
- Red: Write a failing test.
- Green: Make it pass.
- Refactor: Clean up code.
Example in Go
Step 01 —> Write the tests
func TestAdd(t *testing.T) {
result := Add(2, 3)
if result != 5 {
t.Errorf("Expected 5, got %d", result)
}
}Step 2: Run the test – it fails…
Step 3: Write the code
func Add(a, b int) int {
return a + b
}Step 4: Run the test – it passes!
Step 5: Refactor Code..