Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the testing package.
Go
import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated packages like fmt or os instead of testing.
✗ Incorrect
The testing package is imported with "testing" to write tests in Go.
2fill in blank
mediumComplete the function signature for a test function.
Go
func TestExample([1] *testing.T) { } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names or types.
✗ Incorrect
Test functions take a pointer to testing.T named t by convention.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
Log or Errorf which do not stop the test.✗ Incorrect
t.Fatalf logs the error message and stops the test immediately.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names or missing quotes for the subtest name.
✗ Incorrect
t.Run takes a string name and a function with parameter t *testing.T.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names or loop conditions.
✗ Incorrect
Benchmark functions take b *testing.B. The loop uses i and runs b.N times.