Challenge - 5 Problems
Go Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple test function
What is the output when running this Go test code?
Go
package main import ( "testing" ) func TestSum(t *testing.T) { sum := 2 + 3 if sum != 5 { t.Errorf("Expected 5 but got %d", sum) } } func main() {}
Attempts:
2 left
💡 Hint
Tests pass silently unless there is an error.
✗ Incorrect
The test checks if 2 + 3 equals 5, which is true, so the test passes and Go test reports PASS.
❓ Predict Output
intermediate2:00remaining
Behavior of t.Fatal in tests
What happens when t.Fatal is called inside a test function?
Go
package main import ( "testing" ) func TestFailNow(t *testing.T) { t.Fatal("Fatal error occurred") // This line will not run t.Log("This will not print") } func main() {}
Attempts:
2 left
💡 Hint
t.Fatal stops the test immediately after logging.
✗ Incorrect
t.Fatal logs the message and stops the test immediately, so lines after it do not run.
❓ Predict Output
advanced2:00remaining
Output of a benchmark function
What output does this benchmark produce when run with 'go test -bench=.'?
Go
package main import ( "testing" ) func BenchmarkLoop(b *testing.B) { for i := 0; i < b.N; i++ { _ = i * i } } func main() {}
Attempts:
2 left
💡 Hint
Benchmarks report number of iterations and time per operation.
✗ Incorrect
The benchmark runs b.N times and reports ns/op; the test passes if no error occurs.
❓ Predict Output
advanced2:00remaining
Effect of t.Skip in a test
What is the output when t.Skip is called inside a test function?
Go
package main import ( "testing" ) func TestSkipExample(t *testing.T) { t.Skip("Skipping this test") t.Error("This error will not be reported") } func main() {}
Attempts:
2 left
💡 Hint
t.Skip stops the test and marks it as skipped.
✗ Incorrect
t.Skip stops the test immediately and marks it as skipped; errors after it are ignored.
🧠 Conceptual
expert2:00remaining
Understanding parallel tests with t.Parallel
Which statement best describes the behavior of tests that call t.Parallel() in Go?
Attempts:
2 left
💡 Hint
Parallel tests run concurrently but respect the parent test's lifecycle.
✗ Incorrect
t.Parallel marks the test to run concurrently with other parallel tests but waits for the parent test function to complete before finishing.