📖 Scenario: You are working on a simple calculator package in Go. You want to make sure your addition function works correctly by writing a basic test function.
🎯 Goal: Write a basic test function in Go to check if the Add function returns the correct sum.
📋 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.
Test that Add(2, 3) returns 5.
💡 Why This Matters
🌍 Real World
Writing tests helps catch mistakes early and ensures your code works as expected before others use it.
💼 Career
Testing is a key skill for software developers to maintain code quality and reliability in professional projects.
Progress0 / 4 steps
1
Create the Add function
Create a function called Add that takes two integers a and b and returns their sum as an integer.
Go
Hint
Define a function with func Add(a int, b int) int and return a + b.
2
Import testing package
Add an import statement for the testing package below the package main line.
Go
Hint
Use import "testing" to import the testing package.
3
Write the TestAdd function
Write a test function called TestAdd that takes t *testing.T as a parameter. Inside it, call Add(2, 3) and check if the result is 5. If not, use t.Errorf to report an error with the message "Add(2, 3) = %d; want 5".
Go
Hint
Define func TestAdd(t *testing.T), call Add(2, 3), and check if the result is 5. Use t.Errorf to report if not.
4
Run the test and print success message
Add a main function that calls TestAdd with a dummy *testing.T to simulate running the test, then print "Test completed".
Go
Hint
Create a main function that calls TestAdd(&testing.T{}) and prints "Test completed".
Practice
(1/5)
1. Which of the following is the correct way to start a basic test function in Go?
easy
A. func TestExample(t *testing.T) {}
B. func exampleTest(t *testing.T) {}
C. func testExample() {}
D. func ExampleTest(t *testing.T) {}
Solution
Step 1: Understand Go test function naming
Test functions must start with 'Test' and take a '*testing.T' parameter.
Step 2: Check each option
Only func TestExample(t *testing.T) {} matches the required signature and naming convention.
Final Answer:
func TestExample(t *testing.T) {} -> Option A
Quick Check:
Test function starts with 'Test' and uses '*testing.T' [OK]
Hint: Test functions start with 'Test' and accept '*testing.T' [OK]
Common Mistakes:
Not starting function name with 'Test'
Missing '*testing.T' parameter
Using lowercase 'test' prefix
2. Which line correctly reports a test failure inside a Go test function and continues execution?
easy
A. t.Fail()
B. t.Error("failure message")
C. t.Fatal("failure message")
D. t.Log("failure message")
Solution
Step 1: Understand failure reporting methods
Both t.Error and t.Fatal report failures, but t.Error reports and continues, t.Fatal stops immediately.
Step 2: Identify the correct method for reporting failure
t.Error is commonly used to report a failure without stopping the test immediately.
Final Answer:
t.Error("failure message") -> Option B
Quick Check:
Use t.Error to report failure inside tests [OK]
Hint: Use t.Error to report test failures without stopping [OK]
Common Mistakes:
Using t.Log which only logs info
Using t.Fail without message
Confusing t.Fatal with t.Error
3. What will be the output when running this test code with go test?
func TestSum(t *testing.T) {
total := 2 + 2
if total != 4 {
t.Error("Sum is incorrect")
}
}
medium
A. Test fails with message 'Sum is incorrect'
B. Syntax error prevents test from running
C. Test passes with no output
D. Test panics at runtime
Solution
Step 1: Analyze the test logic
The sum 2 + 2 equals 4, so the condition 'total != 4' is false and t.Error is not called.
Step 2: Understand test output behavior
Since no error is reported, the test passes silently with no failure message.
Final Answer:
Test passes with no output -> Option C
Quick Check:
Correct sum means no error reported [OK]
Hint: If condition is false, t.Error is not called, test passes [OK]
A. The test always fails because condition is always true
B. Missing *testing.T parameter in function
C. Function name does not start with 'Test'
D. t.Error should be t.Fail
Solution
Step 1: Analyze the condition inside the test
The condition '1 == 1' is always true, so t.Error is always called.
Step 2: Understand test failure consequence
Because t.Error is called unconditionally, the test always fails, which is likely a logic error.
Final Answer:
The test always fails because condition is always true -> Option A
Quick Check:
Always true condition causes test failure [OK]
Hint: Check if test condition wrongly always triggers failure [OK]
Common Mistakes:
Ignoring always true condition
Confusing t.Error and t.Fail usage
Overlooking function naming correctness
5. You want to write a test function that checks if a function Multiply(a, b int) int returns the correct product. Which test function correctly tests this with failure reporting?
hard
A. func TestMultiply() {
if Multiply(3, 4) != 12 {
t.Error("Expected 12")
}
}
B. func TestMultiply(t *testing.T) {
if Multiply(3, 4) != 7 {
t.Error("Expected 7")
}
}
C. func TestMultiply(t *testing.T) {
if Multiply(3, 4) == 12 {
t.Error("Expected 12")
}
}
D. func TestMultiply(t *testing.T) {
if Multiply(3, 4) != 12 {
t.Error("Expected 12")
}
}
Solution
Step 1: Check function signature and parameter
Test functions must have signature func TestX(t *testing.T). Only options B, C, and D have this.
Step 2: Verify test logic correctness
func TestMultiply(t *testing.T) {
if Multiply(3, 4) != 12 {
t.Error("Expected 12")
}
} checks if Multiply(3,4) != 12 and reports error if true, which is correct. func TestMultiply(t *testing.T) {
if Multiply(3, 4) == 12 {
t.Error("Expected 12")
}
} wrongly reports error if result equals 12. func TestMultiply(t *testing.T) {
if Multiply(3, 4) != 7 {
t.Error("Expected 7")
}
} expects 7, which is incorrect.
Final Answer:
func TestMultiply(t *testing.T) {
if Multiply(3, 4) != 12 {
t.Error("Expected 12")
}
} -> Option D
Quick Check:
Correct signature and logic test for Multiply [OK]
Hint: Test functions need correct signature and proper condition [OK]
Common Mistakes:
Missing *testing.T parameter
Wrong expected value in condition
Inverted condition causing false failures
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.