Challenge - 5 Problems
Test-driven basics 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 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") } }
Attempts:
2 left
💡 Hint
The test checks if Add(2, 3) equals 5 and calls t.Error only if it doesn't.
✗ Incorrect
The function Add correctly returns the sum of 2 and 3, which is 5, so the test passes without errors.
❓ Predict Output
intermediate2: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") } }
Attempts:
2 left
💡 Hint
Look carefully at the last if condition and its expected value.
✗ Incorrect
The last condition expects Multiply(2, 2) to be 5, but it returns 4, so the test fails with that error.
🔧 Debug
advanced2: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") } }
Attempts:
2 left
💡 Hint
Test functions must follow a naming convention to be detected.
✗ Incorrect
Go test only runs functions starting with 'Test' and taking *testing.T as parameter. 'testAdd' is ignored.
📝 Syntax
advanced2:00remaining
Syntax error in test function
Which option contains the correct syntax for a Go test function?
Attempts:
2 left
💡 Hint
Test functions must be exported and accept *testing.T as parameter.
✗ Incorrect
Only option C has the correct function name and parameter type for a Go test function.
🚀 Application
expert2: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) {}
Attempts:
2 left
💡 Hint
Only functions starting with 'Test', exported, and with *testing.T parameter run as tests.
✗ Incorrect
Only TestOne, TestTwo, and TestThree meet all criteria. Others fail naming or signature rules.