Challenge - 5 Problems
Go Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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") }
Attempts:
2 left
💡 Hint
Remember that println outputs to standard output and test failures print error messages.
✗ Incorrect
The test passes because sum equals 5, so no error is printed. The println statement outputs 'TestSum completed' before the test finishes, then 'PASS' is printed by the test runner.
❓ Predict Output
intermediate2: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") } }
Attempts:
2 left
💡 Hint
t.Fatal stops the test immediately and prints the message.
✗ Incorrect
The condition is true, so t.Fatal prints 'Math is broken' and marks the test as failed.
🔧 Debug
advanced2: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") } }
Attempts:
2 left
💡 Hint
Test functions must follow a naming convention to be detected.
✗ Incorrect
Go test looks for functions starting with 'Test' and taking *testing.T as parameter. 'sum' does not start with 'Test', so it is ignored.
📝 Syntax
advanced2: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") }
Attempts:
2 left
💡 Hint
In Go, else must be on the same line as the closing brace of if block.
✗ Incorrect
Go requires the else keyword to be on the same line as the closing brace of the if block. Here, else is on the next line, causing a syntax error.
🚀 Application
expert2: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) {}) }
Attempts:
2 left
💡 Hint
Only functions starting with 'Test' and taking *testing.T run as tests. Subtests count as part of parent test.
✗ Incorrect
TestOne, TestTwo, and TestFive are valid tests. testThree is lowercase, TestFour lacks parameter, so only 3 tests run. Subtest inside TestFive is not counted separately.