0
0
Goprogramming~10 mins

Writing basic test functions 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 basic test function in Go.

Go
func TestAddition(t *[1]) {
    // test code here
}
Drag options to blanks, or click blank then click option'
Atesting.T
Btesting
CTest
DTestT
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong parameter type like 'Test' or 'testing' instead of '*testing.T'.
Forgetting the pointer '*' before 'testing.T'.
2fill in blank
medium

Complete the code to fail the test if the sum is incorrect.

Go
func TestSum(t *testing.T) {
    result := 2 + 2
    if result != 4 {
        t.[1]("Sum is incorrect")
    }
}
Drag options to blanks, or click blank then click option'
AFailNow
BErrorf
CError
DFail
Attempts:
3 left
💡 Hint
Common Mistakes
Using Fail or FailNow without a message.
Using Error which does not accept formatting.
3fill in blank
hard

Fix the error in the test function declaration.

Go
func [1](t *testing.T) {
    product := 3 * 3
    if product != 9 {
        t.Errorf("Product is wrong")
    }
}
Drag options to blanks, or click blank then click option'
ATest_Multiply
BtestMultiply
CTestmultiply
DTestMultiply
Attempts:
3 left
💡 Hint
Common Mistakes
Starting the function name with lowercase 'test'.
Using underscores or incorrect capitalization.
4fill in blank
hard

Fill both blanks to check if the function returns the expected value and fail the test if not.

Go
func TestSubtract(t *testing.T) {
    result := subtract(5, 3)
    if result [1] 2 {
        t.[2]("Expected 2 but got different value")
    }
}
Drag options to blanks, or click blank then click option'
A!=
B==
CErrorf
DFailNow
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' in the condition.
Using 'FailNow' which stops the test immediately without message.
5fill in blank
hard

Fill all three blanks to write a test that checks if the function returns true and fails the test if false.

Go
func TestIsEven(t *testing.T) {
    if ![1](4) {
        t.[2]("4 should be even")
        t.[3]()
    }
}
Drag options to blanks, or click blank then click option'
AisEven
BErrorf
CFailNow
DIsEven
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'IsEven' instead of 'isEven' if the function is lowercase.
Not calling t.FailNow() after reporting the error.