0
0
Goprogramming~20 mins

Why testing is required in Go - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Testing Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why is testing important in software development?

Choose the best reason why testing is important when writing software.

ATo find and fix errors before users encounter them
BTo make the program run faster
CTo reduce the size of the program file
DTo make the code look more complex
Attempts:
2 left
💡 Hint

Think about what happens if bugs are found after users start using the software.

Predict Output
intermediate
2:00remaining
What is the output of this Go test function?

Look at this Go test code. What will it print when run?

Go
package main

import (
	"testing"
)

func Add(a, b int) int {
	return a + b
}

func TestAdd(t *testing.T) {
	result := Add(2, 3)
	if result != 5 {
		t.Errorf("Expected 5 but got %d", result)
	} else {
		println("Test passed")
	}
}
ACompilation error
BExpected 5 but got 6
CTest passed
DNo output
Attempts:
2 left
💡 Hint

Check what the Add function returns and what the test expects.

Predict Output
advanced
2:00remaining
What error does this Go test produce?

What error message will this Go test produce when run?

Go
package main

import "testing"

func Divide(a, b int) int {
	return a / b
}

func TestDivide(t *testing.T) {
	_ = Divide(10, 0)
}
ATest passed with no errors
BNo output
CCompilation error: missing return statement
Dpanic: runtime error: integer divide by zero
Attempts:
2 left
💡 Hint

Think about what happens when dividing by zero in Go.

🧠 Conceptual
advanced
2:00remaining
Which is NOT a benefit of testing?

Choose the option that is NOT a benefit of writing tests for your code.

AImproves code readability automatically
BEnsures code works as expected
CHelps catch bugs early
DFacilitates safe code changes
Attempts:
2 left
💡 Hint

Think about what testing can and cannot do for your code.

🚀 Application
expert
2:00remaining
How many test cases are run by this Go test suite?

Given this Go test code, how many test cases will run?

Go
package main

import "testing"

func IsEven(n int) bool {
	return n%2 == 0
}

func TestIsEven(t *testing.T) {
	tests := []struct {
		input int
		expect bool
	}{
		{2, true},
		{3, false},
		{0, true},
		{-4, true},
	}

	for _, test := range tests {
		if result := IsEven(test.input); result != test.expect {
			t.Errorf("For %d expected %v but got %v", test.input, test.expect, result)
		}
	}
}
A2
B4
C0
D1
Attempts:
2 left
💡 Hint

Count how many test cases are defined in the tests slice.