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.
package main import "testing" func TestAdd(t *testing.T) { if 2+2 != 4 { t.Error("2+2 should be 4") } }
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Run 'go test' | Find TestAdd function | TestAdd found |
| 2 | Execute TestAdd | Check if 2+2 == 4 | True |
| 3 | No error called | Test passes | Pass |
| 4 | Report result | Output test summary | PASS |
| Variable | Start | After Step 2 | Final |
|---|---|---|---|
| t.Error called | false | false | false |
| Test result | not run | running | pass |
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.
go test can run it?Test to be recognized by go test.Test starts with Test exactly, others do not follow the naming convention.Test -> Option Ago test.go test on this test file?package main
import "testing"
func TestSum(t *testing.T) {
if 2+2 != 4 {
t.Error("Sum failed")
}
}go test outputs PASS with timing info.go test from running it:package main
import "testing"
func testAdd(t *testing.T) {
if 1+1 != 2 {
t.Error("Add failed")
}
}testAdd with lowercase 't', so go test ignores it.*testing.T parameter, which is correct except the name.math_test.go inside your module. Which command should you use?go test.go test math_test.go runs tests in the specified file. B uses invalid -file flag, C's go test -v runs all tests verbosely, D's -run matches test names.Pass the test and this pattern moves unknown