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.