TDD is a way to write software where you first write a test that fails, then write code to make the test pass, and finally clean up the code. It helps catch mistakes early and guides design.
Click to reveal answer
beginner
In Go, which package is commonly used for writing tests?
The testing package is used in Go to write tests. It provides tools to create test functions and check results.
Click to reveal answer
beginner
What is the typical order of steps in TDD?
1. Write a failing test. 2. Write code to pass the test. 3. Refactor the code. 4. Repeat.
Click to reveal answer
beginner
How do you name a test function in Go?
Test functions start with Test followed by a name, for example, TestAdd. They take a pointer to testing.T as a parameter.
Click to reveal answer
beginner
Why is writing tests first helpful?
Writing tests first helps you think about what your code should do before writing it. It makes your code clearer and reduces bugs.
Click to reveal answer
What is the first step in Test-Driven Development?
ARun the program
BWrite the full program
CRefactor the code
DWrite a failing test
✗ Incorrect
In TDD, you start by writing a test that fails to define what the code should do.
Which Go package do you import to write tests?
Atesting
Bfmt
Cos
Dnet/http
✗ Incorrect
The testing package provides tools for writing tests in Go.
How should a test function be named in Go?
AtestFunction
BFunctionTest
CTestFunction
DfunctionTest
✗ Incorrect
Test functions in Go start with Test followed by a descriptive name, with uppercase T.
What do you do after making a failing test pass in TDD?
AWrite more tests
BRefactor the code
CDelete the test
DRun the program without tests
✗ Incorrect
After passing the test, you clean up or improve the code without changing its behavior.
Why is TDD useful?
AIt helps catch bugs early
BIt removes the need for comments
CIt makes coding slower
DIt avoids writing tests
✗ Incorrect
TDD helps find mistakes early by writing tests first.
Explain the three main steps of Test-Driven Development.
Think about the cycle of writing tests and code.
You got /3 concepts.
Describe how to write a simple test function in Go using the testing package.
Remember the naming and parameter rules for Go tests.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of writing tests in Go using the testing package?
easy
A. To make the program run faster
B. To add comments to the code
C. To check if the code works correctly by running test functions
D. To create user interfaces
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 testing package does
The testing package 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 C
Quick 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
A. func testMyFunc(t *testing.T) {}
B. func MyFuncTest(t *testing.T) {}
C. func TestingMyFunc(t *testing.T) {}
D. func TestMyFunc(t *testing.T) {}
Solution
Step 1: Recall Go test function naming rules
Test functions must start with "Test" and take a *testing.T parameter.
Step 2: Match the correct function signature
Only func TestMyFunc(t *testing.T) {} follows this rule exactly.
Final Answer:
func TestMyFunc(t *testing.T) {} -> Option D
Quick 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
A. No output
B. PASS
C. Compilation error
D. FAIL with error 'Expected 5'
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 B
Quick 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
A. Multiply function is not defined
B. Missing import for Multiply function
C. Test function name should be lowercase
D. t.Error should be t.Fail
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 A
Quick 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
A. func TestEmpty(t *testing.T) {
if MyFunc() == "" {
t.Error("Result should not be empty")
}
}
B. func TestEmpty(t *testing.T) {
if MyFunc() != "" {
t.Error("Result should be empty")
}
}
C. func TestEmpty(t *testing.T) {
if MyFunc() == nil {
t.Error("Result should not be nil")
}
}
D. func TestEmpty(t *testing.T) {
if MyFunc() == 0 {
t.Error("Result should not be zero")
}
}
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 A
Quick 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 unknownstrongwhile earning +1 Grip toward your certificate.