Test-driven basics help you write code that works right the first time. You write tests before the code to check if it does what you want.
Test-driven basics in Go
Introduction
Syntax
Go
func TestFunctionName(t *testing.T) {
// test code here
}Test functions start with Test and take *testing.T as a parameter.
Use t.Error or t.Fatal to report test failures.
Examples
Add function returns 5 when adding 2 and 3.Go
func TestAdd(t *testing.T) {
got := Add(2, 3)
want := 5
if got != want {
t.Errorf("Add(2, 3) = %d; want %d", got, want)
}
}IsEven correctly identifies even and odd numbers.Go
func TestIsEven(t *testing.T) {
if !IsEven(4) {
t.Error("4 should be even")
}
if IsEven(5) {
t.Error("5 should not be even")
}
}Sample Program
This program defines a simple Add function.
You run the test using go test command in the terminal.
Go
package main func Add(a, b int) int { return a + b } // To run this test, save the test code in a separate file named add_test.go // and run: go test
Important Notes
Test files in Go end with _test.go and are separate from main code files.
Run tests with go test in the terminal inside your project folder.
Tests help you find bugs early and keep your code working as you add features.
Summary
Write tests before or alongside your code to check it works.
Test functions start with Test and use *testing.T to report errors.
Run tests with go test to see if your code passes or fails.
Practice
1. What is the main purpose of writing tests in Go using the
testing package?easy
Solution
Step 1: Understand the role of tests in Go
Tests are written to verify that the code behaves as expected.Step 2: Identify what the
Thetestingpackage doestestingpackage provides tools to write and run tests that check code correctness.Final Answer:
To check if the code works correctly by running test functions -> Option CQuick Check:
Tests check code correctness = B [OK]
Hint: Tests verify code correctness before running fully [OK]
Common Mistakes:
- Thinking tests improve speed
- Confusing tests with comments
- Assuming tests create UI
2. Which of the following is the correct way to start a test function in Go?
easy
Solution
Step 1: Recall Go test function naming rules
Test functions must start with "Test" and take a*testing.Tparameter.Step 2: Match the correct function signature
Onlyfunc TestMyFunc(t *testing.T) {}follows this rule exactly.Final Answer:
func TestMyFunc(t *testing.T) {} -> Option DQuick Check:
Test functions start with "Test" = C [OK]
Hint: Test functions must start with uppercase 'Test' [OK]
Common Mistakes:
- Starting function name with lowercase 'test'
- Adding extra words before 'Test'
- Using wrong parameter types
3. What will be the output when running
go test on this test file?package main
import "testing"
func Add(a, b int) int {
return a + b
}
func TestAdd(t *testing.T) {
if Add(2, 3) != 5 {
t.Error("Expected 5")
}
}medium
Solution
Step 1: Analyze the Add function and test
The Add function returns the sum of 2 and 3, which is 5.Step 2: Check the test condition
The test expects Add(2, 3) to be 5, which is true, so no error is called.Final Answer:
PASS -> Option BQuick Check:
Correct sum means test passes = D [OK]
Hint: If test condition matches, output is PASS [OK]
Common Mistakes:
- Assuming test fails when condition is true
- Thinking test prints 'Expected 5' on success
- Confusing test failure with compile error
4. Identify the error in this test code snippet:
package main
import "testing"
func TestMultiply(t *testing.T) {
result := Multiply(2, 3)
if result != 6 {
t.Error("Expected 6")
}
}medium
Solution
Step 1: Check if Multiply function is defined
The code snippet does not show any definition or import of Multiply function.Step 2: Understand Go requires functions to be defined or imported
Without definition or import, the code will not compile.Final Answer:
Multiply function is not defined -> Option AQuick Check:
Undefined function causes compile error = A [OK]
Hint: Undefined functions cause compile errors [OK]
Common Mistakes:
- Thinking test function names can be lowercase
- Confusing t.Error with t.Fail
- Assuming missing import causes runtime error
5. You want to write a test that fails if a function returns an empty string. Which test code snippet correctly implements this check?
hard
Solution
Step 1: Understand the test goal
The test should fail if the function returns an empty string "".Step 2: Check each option's condition
func TestEmpty(t *testing.T) { if MyFunc() == "" { t.Error("Result should not be empty") } } fails when MyFunc() returns "", which matches the goal.Final Answer:
func TestEmpty(t *testing.T) { if MyFunc() == "" { t.Error("Result should not be empty") } } -> Option AQuick Check:
Fail on empty string = A [OK]
Hint: Fail test if function returns empty string "" [OK]
Common Mistakes:
- Checking for nil instead of empty string
- Checking for zero instead of string
- Reversing the condition logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
