Bird
Raised Fist0
Goprogramming~20 mins

Running tests in Go - Practice Problems & Coding Challenges

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

import (
	"testing"
)

func TestSum(t *testing.T) {
	sum := 2 + 3
	if sum != 5 {
		t.Errorf("Expected 5 but got %d", sum)
	}
	println("TestSum completed")
}
A
FAIL
Expected 5 but got 5
B
TestSum completed
PASS
CNo output
D
PASS
TestSum completed
Attempts:
2 left
💡 Hint
Remember that println outputs to standard output and test failures print error messages.
❓ Predict Output
intermediate
2:00remaining
What error does this Go test produce?
What error message will this Go test produce when run?
Go
package main

import "testing"

func TestFail(t *testing.T) {
	if 1+1 != 3 {
		t.Fatal("Math is broken")
	}
}
A
Math is broken
FAIL
BPASS
Cpanic: Math is broken
DNo output
Attempts:
2 left
💡 Hint
t.Fatal stops the test immediately and prints the message.
🔧 Debug
advanced
2:00remaining
Why does this Go test not run any tests?
Given this Go test file, why does running go test report 'no tests to run'?
Go
package main

import "testing"

func sum(t *testing.T) {
	if 2+2 != 4 {
		t.Error("Wrong sum")
	}
}
ATest functions cannot take parameters
BTest function must return an error
CFunction name must start with 'Test' to be recognized as a test
DMissing import of 'fmt' package
Attempts:
2 left
💡 Hint
Test functions must follow a naming convention to be detected.
📝 Syntax
advanced
2:00remaining
What syntax error does this Go test code produce?
Identify the syntax error in this Go test code snippet.
Go
package main

import "testing"

func TestExample(t *testing.T) {
	if 1 == 1 {
		t.Log("One equals one")
	} else
		t.Error("Math error")
}
ANo syntax error, code runs fine
BSyntax error: missing parentheses around else condition
CSyntax error: missing semicolon after t.Log call
DSyntax error: else must be on the same line as closing brace
Attempts:
2 left
💡 Hint
In Go, else must be on the same line as the closing brace of if block.
🚀 Application
expert
2:00remaining
How many tests are run by this Go test file?
Given this Go test file, how many tests will go test run?
Go
package main

import "testing"

func TestOne(t *testing.T) {}

func TestTwo(t *testing.T) {}

func testThree(t *testing.T) {}

func TestFour() {}

func TestFive(t *testing.T) {
	t.Run("Subtest", func(t *testing.T) {})
}
A3
B4
C5
D2
Attempts:
2 left
💡 Hint
Only functions starting with 'Test' and taking *testing.T run as tests. Subtests count as part of parent test.

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

  1. Step 1: Understand Go test naming rules

    Go requires test functions to start with an uppercase Test to be recognized by go test.
  2. Step 2: Check the options

    Only Start the function name with Test starts with Test exactly, others do not follow the naming convention.
  3. Final Answer:

    Start the function name with Test -> Option A
  4. 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

  1. Step 1: Recall Go test command

    The command to run tests in Go is go test.
  2. Step 2: Compare options

    Only go test matches the correct command. Others are invalid commands.
  3. Final Answer:

    go test -> Option D
  4. 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

  1. Step 1: Analyze the test logic

    The test checks if 2+2 equals 4, which is true, so no error is called.
  2. Step 2: Understand test output

    Since the test passes, go test outputs PASS with timing info.
  3. Final Answer:

    PASS ok main 0.001s -> Option C
  4. 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

  1. Step 1: Check test function naming

    The function is named testAdd with lowercase 't', so go test ignores it.
  2. Step 2: Verify other requirements

    Test functions must start with uppercase 'Test' and accept *testing.T parameter, which is correct except the name.
  3. Final Answer:

    Function name must start with uppercase 'Test' -> Option B
  4. 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

  1. Step 1: Understand how to run tests in a specific file

    Go allows specifying test files directly as arguments to go test.
  2. 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.
  3. Final Answer:

    go test math_test.go -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard