0
0
Goprogramming~10 mins

Test-driven basics 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 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'
A[]
B{}
C<>
D()
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces {} instead of parentheses () after the function name.
Omitting the parentheses completely.
2fill in blank
medium

Complete the code to import the testing package in Go.

Go
[1] "testing"
Drag options to blanks, or click blank then click option'
A""
Bpackage
Cimport
D_
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'package', which declares the current package name.
Using '_' for blank imports, which prevents using 'testing.T'.
3fill in blank
hard

Fix 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'
A5
B6
C0
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing sum to the wrong number.
Using sum instead of a number in the condition.
4fill in blank
hard

Fill 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'
A()
B5
C6
D{}
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses after the function name.
Using the wrong expected value in the if condition.
5fill in blank
hard

Fill 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'
A()
B6
Cwant
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong expected value.
Comparing got to a literal instead of want.