0
0
Goprogramming~10 mins

Test-driven basics in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Test-driven basics
Write a failing test
Run test: it fails
Write code to pass test
Run test: it passes
Refactor code if needed
Repeat for next feature
This flow shows how you first write a test that fails, then write code to pass it, run tests to confirm, and finally improve code.
Execution Sample
Go
package main
import "testing"

func Add(a, b int) int {
    return a + b
}

// Test
func TestAdd(t *testing.T) {
    if Add(2, 3) != 5 {
        t.Error("Expected 5")
    }
}
This Go code defines a simple Add function and a test that checks if Add(2,3) returns 5.
Execution Table
StepActionTest ResultCode State
1Write TestAdd checking Add(2,3)==5No test run yetAdd function not implemented or empty
2Run testsFail: Add returns 0 or no codeAdd function empty or wrong
3Write Add function returning a+bCode updated to correct logicAdd returns sum of inputs
4Run tests againPass: Add(2,3) == 5Code correct, test passes
5Refactor code if neededTests still passCode clean and working
6EndAll tests pass, development doneReady for next feature
💡 All tests pass, so development cycle completes successfully
Variable Tracker
VariableStartAfter Step 3After Step 4Final
Add(2,3) resultundefined555
Test statusnot runnot runpasspass
Key Moments - 3 Insights
Why does the test fail at first before writing the Add function?
Because the Add function is not implemented or returns wrong value, so the test checking Add(2,3)==5 fails as shown in execution_table step 2.
Why do we run tests again after writing the Add function?
To confirm the new code works and the test passes, as shown in execution_table step 4 where the test passes.
What is the purpose of refactoring after tests pass?
To improve code quality without changing behavior, ensuring tests still pass as in execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the test result at step 2?
ANo test run yet
BPass: Add(2,3) == 5
CFail: Add returns 0 or no code
DTests skipped
💡 Hint
Check the 'Test Result' column at step 2 in execution_table
At which step does the Add function get implemented correctly?
AStep 3
BStep 1
CStep 4
DStep 5
💡 Hint
Look at 'Code State' column to see when Add returns sum of inputs
If the Add function returned a fixed number 10 instead of a+b, what would happen at step 4?
ATest would pass
BTest would fail
CTest would not run
DTest would be skipped
💡 Hint
Refer to variable_tracker where Add(2,3) result must be 5 to pass test
Concept Snapshot
Test-driven basics in Go:
1. Write a test that fails.
2. Run tests to confirm failure.
3. Write code to pass the test.
4. Run tests to confirm pass.
5. Refactor code while tests pass.
Repeat for new features.
Full Transcript
Test-driven basics means first writing a test that checks what your code should do. Initially, the test fails because the code is missing or wrong. Then you write the code to make the test pass. You run the tests again to confirm success. After that, you can clean up your code without breaking the test. This cycle repeats for each new feature or fix. The example shows a simple Add function and its test in Go. The test first fails, then passes after writing Add, demonstrating the test-driven process.