0
0
Goprogramming~10 mins

Running tests in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Running tests
Write test function
Run 'go test'
Test framework executes test
Check test result
Report success
This flow shows how Go runs tests: you write test functions, run 'go test', it executes tests, then reports pass or fail.
Execution Sample
Go
package main
import "testing"
func TestAdd(t *testing.T) {
  if 2+2 != 4 {
    t.Error("2+2 should be 4")
  }
}
This test checks if 2+2 equals 4 and reports an error if not.
Execution Table
StepActionEvaluationResult
1Run 'go test'Find TestAdd functionTestAdd found
2Execute TestAddCheck if 2+2 == 4True
3No error calledTest passesPass
4Report resultOutput test summaryPASS
💡 TestAdd completes without error, so test passes and 'go test' reports success.
Variable Tracker
VariableStartAfter Step 2Final
t.Error calledfalsefalsefalse
Test resultnot runrunningpass
Key Moments - 2 Insights
Why does the test pass even though there is no explicit 'return true'?
In Go tests, if t.Error is not called, the test passes by default as shown in execution_table step 3.
What happens if the condition 2+2 != 4 is true?
If true, t.Error is called, marking the test as failed, which would be shown in execution_table step 3 as a failure.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result after executing TestAdd?
APass
BFail
CError
DSkipped
💡 Hint
Check execution_table row 3 where the test result is 'Pass'.
At which step does the test framework find the test function?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
See execution_table row 1 where 'TestAdd found' is noted.
If the condition 2+2 != 4 was true, how would the variable 't.Error called' change?
AIt would stay false
BIt would be undefined
CIt would become true
DIt would reset to false
💡 Hint
Refer to variable_tracker and key_moments explaining t.Error call on failure.
Concept Snapshot
Go tests are functions starting with Test.
Run tests using 'go test' command.
If t.Error or t.Fail is called, test fails.
If no error, test passes by default.
Test results are reported after execution.
Full Transcript
In Go, you write test functions named starting with Test. When you run 'go test', the tool finds these functions and runs them. Inside a test, you check conditions. If a condition fails, you call t.Error to mark failure. If no errors occur, the test passes. The tool then reports the results. This example shows a test checking if 2+2 equals 4. Since it does, no error is called and the test passes.