Bird
Raised Fist0
Goprogramming~20 mins

Test-driven basics in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Test-driven basics Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple test function
What will be the output when running this Go test code 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")
    }
}
ANo output, test file not recognized
B
FAIL
--- FAIL: TestAdd (0.00s)
    main_test.go:9: Expected 5
Cpanic: runtime error: invalid memory address or nil pointer dereference
D
PASS
ok  	command-line-arguments	0.001s
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
Test failure output
What output will this test produce 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, -1) != -2 {
        t.Error("Expected -2")
    }
    if Multiply(2, 2) != 5 {
        t.Error("Expected 5")
    }
}
A
FAIL
--- FAIL: TestMultiply (0.00s)
    main_test.go:15: Expected 5
B
PASS
ok  	command-line-arguments	0.001s
C
FAIL
--- FAIL: TestMultiply (0.00s)
    main_test.go:9: Expected 6
Dpanic: index out of range
Attempts:
2 left
💡 Hint
Look carefully at the last if condition and its expected value.
🔧 Debug
advanced
2:00remaining
Identify the test bug causing no tests to run
Why does this test file produce 'ok command-line-arguments 0.001s' without running any tests?
Go
package main

import "testing"

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

func testAdd(t *testing.T) {
    if add(1, 2) != 3 {
        t.Error("Expected 3")
    }
}
AThe test function name must start with uppercase 'Test' to be recognized by go test.
BThe add function is unexported and cannot be tested.
CThe testing package is not imported correctly.
DThe package name must be 'main_test' for tests to run.
Attempts:
2 left
💡 Hint
Test functions must follow a naming convention to be detected.
📝 Syntax
advanced
2:00remaining
Syntax error in test function
Which option contains the correct syntax for a Go test function?
A
func testSum(t *testing.T) {
    if Sum(1, 2) != 3 {
        t.Error("fail")
    }
}
B
func TestSum() {
    if Sum(1, 2) != 3 {
        t.Error("fail")
    }
}
C
func TestSum(t *testing.T) {
    if Sum(1, 2) != 3 {
        t.Error("fail")
    }
}
D
func TestSum(t testing.T) {
    if Sum(1, 2) != 3 {
        t.Error("fail")
    }
}
Attempts:
2 left
💡 Hint
Test functions must be exported and accept *testing.T as parameter.
🚀 Application
expert
2:00remaining
Number of tests run in a file
Given this test file, how many tests will be executed when running go test?
Go
package main

import "testing"

func TestOne(t *testing.T) {}
func TestTwo(t *testing.T) {}
func TestThree(t *testing.T) {}
func testFour(t *testing.T) {}
func TestFive() {}
func TestSix(t testing.T) {}
A4
B3
C5
D6
Attempts:
2 left
💡 Hint
Only functions starting with 'Test', exported, and with *testing.T parameter run as tests.

Practice

(1/5)
1. What is the main purpose of writing tests in Go using the testing package?
easy
A. To make the program run faster
B. To add comments to the code
C. To check if the code works correctly by running test functions
D. To create user interfaces

Solution

  1. Step 1: Understand the role of tests in Go

    Tests are written to verify that the code behaves as expected.
  2. Step 2: Identify what the testing package does

    The testing package provides tools to write and run tests that check code correctness.
  3. Final Answer:

    To check if the code works correctly by running test functions -> Option C
  4. Quick Check:

    Tests check code correctness = B [OK]
Hint: Tests verify code correctness before running fully [OK]
Common Mistakes:
  • Thinking tests improve speed
  • Confusing tests with comments
  • Assuming tests create UI
2. Which of the following is the correct way to start a test function in Go?
easy
A. func testMyFunc(t *testing.T) {}
B. func MyFuncTest(t *testing.T) {}
C. func TestingMyFunc(t *testing.T) {}
D. func TestMyFunc(t *testing.T) {}

Solution

  1. Step 1: Recall Go test function naming rules

    Test functions must start with "Test" and take a *testing.T parameter.
  2. Step 2: Match the correct function signature

    Only func TestMyFunc(t *testing.T) {} follows this rule exactly.
  3. Final Answer:

    func TestMyFunc(t *testing.T) {} -> Option D
  4. Quick Check:

    Test functions start with "Test" = C [OK]
Hint: Test functions must start with uppercase 'Test' [OK]
Common Mistakes:
  • Starting function name with lowercase 'test'
  • Adding extra words before 'Test'
  • Using wrong parameter types
3. What will be the output when running go test on this test file?
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")
    }
}
medium
A. No output
B. PASS
C. Compilation error
D. FAIL with error 'Expected 5'

Solution

  1. Step 1: Analyze the Add function and test

    The Add function returns the sum of 2 and 3, which is 5.
  2. Step 2: Check the test condition

    The test expects Add(2, 3) to be 5, which is true, so no error is called.
  3. Final Answer:

    PASS -> Option B
  4. Quick Check:

    Correct sum means test passes = D [OK]
Hint: If test condition matches, output is PASS [OK]
Common Mistakes:
  • Assuming test fails when condition is true
  • Thinking test prints 'Expected 5' on success
  • Confusing test failure with compile error
4. Identify the error in this test code snippet:
package main

import "testing"

func TestMultiply(t *testing.T) {
    result := Multiply(2, 3)
    if result != 6 {
        t.Error("Expected 6")
    }
}
medium
A. Multiply function is not defined
B. Missing import for Multiply function
C. Test function name should be lowercase
D. t.Error should be t.Fail

Solution

  1. Step 1: Check if Multiply function is defined

    The code snippet does not show any definition or import of Multiply function.
  2. Step 2: Understand Go requires functions to be defined or imported

    Without definition or import, the code will not compile.
  3. Final Answer:

    Multiply function is not defined -> Option A
  4. Quick Check:

    Undefined function causes compile error = A [OK]
Hint: Undefined functions cause compile errors [OK]
Common Mistakes:
  • Thinking test function names can be lowercase
  • Confusing t.Error with t.Fail
  • Assuming missing import causes runtime error
5. You want to write a test that fails if a function returns an empty string. Which test code snippet correctly implements this check?
hard
A. func TestEmpty(t *testing.T) { if MyFunc() == "" { t.Error("Result should not be empty") } }
B. func TestEmpty(t *testing.T) { if MyFunc() != "" { t.Error("Result should be empty") } }
C. func TestEmpty(t *testing.T) { if MyFunc() == nil { t.Error("Result should not be nil") } }
D. func TestEmpty(t *testing.T) { if MyFunc() == 0 { t.Error("Result should not be zero") } }

Solution

  1. Step 1: Understand the test goal

    The test should fail if the function returns an empty string "".
  2. Step 2: Check each option's condition

    func TestEmpty(t *testing.T) { if MyFunc() == "" { t.Error("Result should not be empty") } } fails when MyFunc() returns "", which matches the goal.
  3. Final Answer:

    func TestEmpty(t *testing.T) { if MyFunc() == "" { t.Error("Result should not be empty") } } -> Option A
  4. Quick Check:

    Fail on empty string = A [OK]
Hint: Fail test if function returns empty string "" [OK]
Common Mistakes:
  • Checking for nil instead of empty string
  • Checking for zero instead of string
  • Reversing the condition logic
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