📖 Scenario: You are working on a small Go program that adds two numbers. To make sure your code works correctly, you want to write a simple test.
🎯 Goal: Create a Go function that adds two integers, then write a test function to check if it returns the correct result.
📋 What You'll Learn
Create a function called Add that takes two integers and returns their sum.
Create a test function called TestAdd in the same package.
Use the testing package and t.Errorf to report errors.
Run the test using go test and print the result.
💡 Why This Matters
🌍 Real World
Writing tests helps catch mistakes early and makes your Go programs more reliable.
💼 Career
Testing is a key skill for software developers to ensure code quality and maintainability.
Progress0 / 4 steps
1
Create the Add function
Write a function called Add that takes two integers named a and b and returns their sum as an integer.
Go
Hint
Define a function with two parameters and return their sum using return a + b.
2
Create the test function
Import the testing package and write a test function called TestAdd that takes t *testing.T as a parameter.
Go
Hint
Use import "testing" and define func TestAdd(t *testing.T).
3
Add a test case inside TestAdd
Inside TestAdd, call Add(2, 3) and check if the result equals 5. If not, use t.Errorf to report an error with the message "Add(2, 3) = %d; want 5".
Go
Hint
Store the result of Add(2, 3) in a variable, then compare it to 5. Use t.Errorf to show an error if it does not match.
4
Run the test and print the result
Run the test using the command go test in your terminal. Then write a comment showing the expected output: ok [package_name] 0.XXXs if the test passes.
Go
Hint
Open your terminal, run go test in the folder with your code, and check that the output shows ok meaning the test passed.
Practice
(1/5)
1. What is the correct way to name a test function in Go so that go test can run it?
easy
A. Start the function name with Test
B. Start the function name with test (lowercase t)
C. Name the function RunTest
D. Name the function CheckTest
Solution
Step 1: Understand Go test naming rules
Go requires test functions to start with an uppercase Test to be recognized by go test.
Step 2: Check the options
Only Start the function name with Test starts with Test exactly, others do not follow the naming convention.
Final Answer:
Start the function name with Test -> Option A
Quick Check:
Test functions start with 'Test' [OK]
Hint: Test functions must start with uppercase 'Test' [OK]
Common Mistakes:
Using lowercase 'test' at start
Naming test functions without 'Test' prefix
Using unrelated prefixes like 'Run' or 'Check'
2. Which of the following is the correct syntax to run tests in a Go module from the command line?
easy
A. go execute test
B. go run test
C. go build test
D. go test
Solution
Step 1: Recall Go test command
The command to run tests in Go is go test.
Step 2: Compare options
Only go test matches the correct command. Others are invalid commands.
Final Answer:
go test -> Option D
Quick Check:
Run tests with 'go test' [OK]
Hint: Use 'go test' to run tests, not 'go run' [OK]
Common Mistakes:
Using 'go run test' instead of 'go test'
Trying 'go build test' which builds but doesn't test
Using non-existent commands like 'go execute test'
3. What will be the output when running go test on this test file?
package main
import "testing"
func TestSum(t *testing.T) {
if 2+2 != 4 {
t.Error("Sum failed")
}
}
medium
A. Syntax error: missing main function
B. FAIL
--- FAIL: TestSum
C. PASS
ok main 0.001s
D. No tests to run
Solution
Step 1: Analyze the test logic
The test checks if 2+2 equals 4, which is true, so no error is called.
Step 2: Understand test output
Since the test passes, go test outputs PASS with timing info.
Final Answer:
PASS
ok main 0.001s -> Option C
Quick Check:
Test passes, so output shows PASS [OK]
Hint: If test condition is true, output shows PASS [OK]
Common Mistakes:
Assuming syntax error due to missing main
Expecting FAIL when condition is true
Thinking tests won't run without main function
4. Identify the error in this test function that prevents go test from running it:
package main
import "testing"
func testAdd(t *testing.T) {
if 1+1 != 2 {
t.Error("Add failed")
}
}
medium
A. Missing import of 'fmt' package
B. Function name must start with uppercase 'Test'
C. Test function must return a value
D. Test functions cannot have parameters
Solution
Step 1: Check test function naming
The function is named testAdd with lowercase 't', so go test ignores it.
Step 2: Verify other requirements
Test functions must start with uppercase 'Test' and accept *testing.T parameter, which is correct except the name.
Final Answer:
Function name must start with uppercase 'Test' -> Option B
Quick Check:
Test functions need 'Test' prefix [OK]
Hint: Test functions must start with uppercase 'Test' [OK]
Common Mistakes:
Thinking test functions need to return a value
Assuming missing 'fmt' import causes test failure
Believing test functions cannot have parameters
5. You want to run only tests in the file math_test.go inside your module. Which command should you use?
hard
A. go test math_test.go
B. go test -file math_test.go
C. go test -v
D. go test -run math_test.go
Solution
Step 1: Understand how to run tests in a specific file
Go allows specifying test files directly as arguments to go test.
Step 2: Check options for correct usage
Only 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.
Final Answer:
go test math_test.go -> Option A
Quick Check:
Specify test file directly to run its tests [OK]
Hint: Run tests in a file by naming it after 'go test' [OK]
Common Mistakes:
Using invalid flags like -run or -file with filenames
Confusing verbose flag -v with file selection
Not specifying the file and running all tests
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.