0
0
Goprogramming~20 mins

Writing basic test functions in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Go Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple Go test function
What will be the output when running this Go test function 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")
    }
}
Apanic: runtime error: invalid memory address
B
PASS
ok   	command-line-arguments	0.001s
C
FAIL
--- FAIL: TestAdd (0.00s)
    main_test.go:9: Expected 5
DNo output, test file not recognized
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
Result of a failing Go test function
What is the output of this Go test function 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, 2) != 5 {
        t.Error("Expected 5")
    }
}
Apanic: index out of range
B
PASS
ok   	command-line-arguments	0.001s
C
FAIL
--- FAIL: TestMultiply (0.00s)
    main_test.go:10: Expected 6
D
FAIL
--- FAIL: TestMultiply (0.00s)
    main_test.go:13: Expected 5
Attempts:
2 left
💡 Hint
Check which condition in the test fails based on the Multiply function's return values.
🔧 Debug
advanced
2:00remaining
Identify the error in this Go test function
What error will this Go test function produce when run with go test?
Go
package main

import "testing"

func Subtract(a, b int) int {
    return a - b
}

func TestSubtract(t *testing.T) {
    if Subtract(5, 3) != 2 {
        t.Error("Expected 2")
    }
}
ASyntax error: missing { after if condition
BRuntime panic: nil pointer dereference
CPASS with no errors
DTest fails with message: Expected 2
Attempts:
2 left
💡 Hint
Check the if statement syntax carefully.
📝 Syntax
advanced
2:00remaining
Correct syntax for a Go test function
Which option shows the correct syntax for a Go test function named TestDivide that tests a Divide function?
A
func TestDivide(t *testing.T) {
    if Divide(6, 2) != 3 {
        t.Error("Expected 3")
    }
}
B
func testDivide(t *testing.T) {
    if Divide(6, 2) != 3 {
        t.Error("Expected 3")
    }
}
C
func TestDivide() {
    if Divide(6, 2) != 3 {
        t.Error("Expected 3")
    }
}
D
func TestDivide(t testing.T) {
    if Divide(6, 2) != 3 {
        t.Error("Expected 3")
    }
}
Attempts:
2 left
💡 Hint
Test functions must start with 'Test' and take *testing.T as parameter.
🚀 Application
expert
2:00remaining
Number of tests run and output from multiple test functions
Given these two Go test functions in the same package, what will be the output summary after running go test?
Go
package main

import "testing"

func TestOne(t *testing.T) {
    t.Log("Running TestOne")
}

func TestTwo(t *testing.T) {
    t.Error("TestTwo failed")
}
A
FAIL
--- FAIL: TestOne (0.00s)
    main_test.go:7: Running TestOne
FAIL
exit status 1
FAIL	command-line-arguments	0.002s
B
PASS
ok   	command-line-arguments	0.002s
C
FAIL
--- FAIL: TestTwo (0.00s)
    main_test.go:10: TestTwo failed
FAIL
exit status 1
FAIL	command-line-arguments	0.002s
D
PASS
--- PASS: TestOne (0.00s)
--- PASS: TestTwo (0.00s)
ok   	command-line-arguments	0.002s
Attempts:
2 left
💡 Hint
t.Error causes test failure; t.Log only logs info without failing.