0
0
Goprogramming~10 mins

Testing package overview in Go - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the testing package.

Go
import [1]
Drag options to blanks, or click blank then click option'
A"testing"
B"fmt"
C"os"
D"net/http"
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated packages like fmt or os instead of testing.
2fill in blank
medium

Complete the function signature for a test function.

Go
func TestExample([1] *testing.T) { }
Drag options to blanks, or click blank then click option'
Atesting
Bt
Ctest
DT
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names or types.
3fill in blank
hard

Fix the error in the test function to fail the test on error.

Go
func TestFail(t *testing.T) {
    if err != nil {
        t.[1]("Error occurred")
    }
}
Drag options to blanks, or click blank then click option'
AErrorf
BLog
CFatalf
DFailNow
Attempts:
3 left
💡 Hint
Common Mistakes
Using Log or Errorf which do not stop the test.
4fill in blank
hard

Fill both blanks to create a subtest with a name and a function.

Go
t.Run([1], func([2] *testing.T) {
    // test code
})
Drag options to blanks, or click blank then click option'
A"SubTest1"
Bt
Ctest
D"Test"
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names or missing quotes for the subtest name.
5fill in blank
hard

Fill all three blanks to create a benchmark function with a loop.

Go
func BenchmarkExample([1] *testing.B) {
    for [2] := 0; [2] < [3].N; [2]++ {
        // benchmark code
    }
}
Drag options to blanks, or click blank then click option'
Ab
Bi
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names or loop conditions.