Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a test function in Go.
Go
func TestSum[1](t *testing.T) {
// test code here
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces {} instead of parentheses () after the function name.
Omitting the parentheses completely.
✗ Incorrect
In Go, test functions must have the form func TestXxx(t *testing.T) with parentheses after the function name.
2fill in blank
mediumComplete the code to import the testing package in Go.
Go
[1] "testing"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'package', which declares the current package name.
Using '_' for blank imports, which prevents using 'testing.T'.
✗ Incorrect
The standard way to import the testing package is 'import "testing"' so that you can use types like testing.T in your tests.
3fill in blank
hardFix the error in the test assertion to fail the test if sum is not 5.
Go
if sum != [1] { t.Errorf("Expected 5 but got %d", sum) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing sum to the wrong number.
Using sum instead of a number in the condition.
✗ Incorrect
The test should check if sum is not equal to 5 to detect failure correctly.
4fill in blank
hardFill both blanks to create a simple test that checks if Add(2, 3) equals 5.
Go
func TestAdd[1](t *testing.T) { result := Add(2, 3) if result != [2] { t.Errorf("Expected 5 but got %d", result) } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses after the function name.
Using the wrong expected value in the if condition.
✗ Incorrect
Test functions need parentheses after their name, and the expected result is 5.
5fill in blank
hardFill all three blanks to write a test that fails if Multiply(2, 3) is not 6.
Go
func TestMultiply[1](t *testing.T) { got := Multiply(2, 3) want := [2] if got != [3] { t.Errorf("Expected %d but got %d", want, got) } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong expected value.
Comparing got to a literal instead of want.
✗ Incorrect
Test function needs parentheses, expected value is 6, and the if condition compares got to want.