Bird
Raised Fist0
Goprogramming~20 mins

Writing basic test functions in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Go Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple Go test function
What will be the output when running this Go test function with go test?
Go
package main

import "testing"

func Add(a, b int) int {
    return a + b
}

func TestAdd(t *testing.T) {
    if Add(2, 3) != 5 {
        t.Error("Expected 5")
    }
}
Apanic: runtime error: invalid memory address
B
PASS
ok   	command-line-arguments	0.001s
C
FAIL
--- FAIL: TestAdd (0.00s)
    main_test.go:9: Expected 5
DNo output, test file not recognized
Attempts:
2 left
💡 Hint
The test checks if Add(2, 3) equals 5 and calls t.Error only if it doesn't.
❓ Predict Output
intermediate
2:00remaining
Result of a failing Go test function
What is the output of this Go test function when run with go test?
Go
package main

import "testing"

func Multiply(a, b int) int {
    return a * b
}

func TestMultiply(t *testing.T) {
    if Multiply(2, 3) != 6 {
        t.Error("Expected 6")
    }
    if Multiply(2, 0) != 0 {
        t.Error("Expected 0")
    }
    if Multiply(2, 2) != 5 {
        t.Error("Expected 5")
    }
}
Apanic: index out of range
B
PASS
ok   	command-line-arguments	0.001s
C
FAIL
--- FAIL: TestMultiply (0.00s)
    main_test.go:10: Expected 6
D
FAIL
--- FAIL: TestMultiply (0.00s)
    main_test.go:13: Expected 5
Attempts:
2 left
💡 Hint
Check which condition in the test fails based on the Multiply function's return values.
🔧 Debug
advanced
2:00remaining
Identify the error in this Go test function
What error will this Go test function produce when run with go test?
Go
package main

import "testing"

func Subtract(a, b int) int {
    return a - b
}

func TestSubtract(t *testing.T) {
    if Subtract(5, 3) != 2 {
        t.Error("Expected 2")
    }
}
ASyntax error: missing { after if condition
BRuntime panic: nil pointer dereference
CPASS with no errors
DTest fails with message: Expected 2
Attempts:
2 left
💡 Hint
Check the if statement syntax carefully.
📝 Syntax
advanced
2:00remaining
Correct syntax for a Go test function
Which option shows the correct syntax for a Go test function named TestDivide that tests a Divide function?
A
func TestDivide(t *testing.T) {
    if Divide(6, 2) != 3 {
        t.Error("Expected 3")
    }
}
B
func testDivide(t *testing.T) {
    if Divide(6, 2) != 3 {
        t.Error("Expected 3")
    }
}
C
func TestDivide() {
    if Divide(6, 2) != 3 {
        t.Error("Expected 3")
    }
}
D
func TestDivide(t testing.T) {
    if Divide(6, 2) != 3 {
        t.Error("Expected 3")
    }
}
Attempts:
2 left
💡 Hint
Test functions must start with 'Test' and take *testing.T as parameter.
🚀 Application
expert
2:00remaining
Number of tests run and output from multiple test functions
Given these two Go test functions in the same package, what will be the output summary after running go test?
Go
package main

import "testing"

func TestOne(t *testing.T) {
    t.Log("Running TestOne")
}

func TestTwo(t *testing.T) {
    t.Error("TestTwo failed")
}
A
FAIL
--- FAIL: TestOne (0.00s)
    main_test.go:7: Running TestOne
FAIL
exit status 1
FAIL	command-line-arguments	0.002s
B
PASS
ok   	command-line-arguments	0.002s
C
FAIL
--- FAIL: TestTwo (0.00s)
    main_test.go:10: TestTwo failed
FAIL
exit status 1
FAIL	command-line-arguments	0.002s
D
PASS
--- PASS: TestOne (0.00s)
--- PASS: TestTwo (0.00s)
ok   	command-line-arguments	0.002s
Attempts:
2 left
💡 Hint
t.Error causes test failure; t.Log only logs info without failing.

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

  1. Step 1: Understand Go test function naming

    Test functions must start with 'Test' and take a '*testing.T' parameter.
  2. Step 2: Check each option

    Only func TestExample(t *testing.T) {} matches the required signature and naming convention.
  3. Final Answer:

    func TestExample(t *testing.T) {} -> Option A
  4. 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

  1. Step 1: Understand failure reporting methods

    Both t.Error and t.Fatal report failures, but t.Error reports and continues, t.Fatal stops immediately.
  2. Step 2: Identify the correct method for reporting failure

    t.Error is commonly used to report a failure without stopping the test immediately.
  3. Final Answer:

    t.Error("failure message") -> Option B
  4. 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

  1. 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.
  2. Step 2: Understand test output behavior

    Since no error is reported, the test passes silently with no failure message.
  3. Final Answer:

    Test passes with no output -> Option C
  4. Quick Check:

    Correct sum means no error reported [OK]
Hint: If condition is false, t.Error is not called, test passes [OK]
Common Mistakes:
  • Assuming test fails when condition is false
  • Confusing test output with print statements
  • Thinking syntax error exists when code is correct
4. Identify the error in this test function:
func TestCheck(t *testing.T) {
    if 1 == 1 {
        t.Error("Test failed")
    }
}
medium
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

  1. Step 1: Analyze the condition inside the test

    The condition '1 == 1' is always true, so t.Error is always called.
  2. Step 2: Understand test failure consequence

    Because t.Error is called unconditionally, the test always fails, which is likely a logic error.
  3. Final Answer:

    The test always fails because condition is always true -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    func TestMultiply(t *testing.T) { if Multiply(3, 4) != 12 { t.Error("Expected 12") } } -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard