Running tests in Go - Time & Space Complexity
When running tests in Go, it's important to know how the time needed grows as tests increase.
We want to see how the total test time changes when we add more tests.
Analyze the time complexity of the following code snippet.
func TestAll(t *testing.T) {
for i := 0; i < len(testCases); i++ {
i := i // capture loop variable
t.Run(testCases[i].name, func(t *testing.T) {
result := SomeFunction(testCases[i].input)
if result != testCases[i].expected {
t.Errorf("got %v, want %v", result, testCases[i].expected)
}
})
}
}
This code runs a series of tests, one for each case in the testCases list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each test case and running a test.
- How many times: Once for each test case in the list.
Each test case adds one more test to run, so the total time grows as the number of tests grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Runs 10 tests |
| 100 | Runs 100 tests |
| 1000 | Runs 1000 tests |
Pattern observation: The total work grows directly with the number of tests.
Time Complexity: O(n)
This means the total test time grows in a straight line as you add more tests.
[X] Wrong: "Running more tests doesn't affect total time much because tests run fast."
[OK] Correct: Even if each test is quick, adding many tests adds up, so total time grows with the number of tests.
Understanding how test time grows helps you write efficient tests and manage testing time well in real projects.
"What if each test case itself runs a loop over data? How would the time complexity change?"
Practice
go test can run it?Solution
Step 1: Understand Go test naming rules
Go requires test functions to start with an uppercaseTestto be recognized bygo test.Step 2: Check the options
Only Start the function name withTeststarts withTestexactly, others do not follow the naming convention.Final Answer:
Start the function name withTest-> Option AQuick Check:
Test functions start with 'Test' [OK]
- Using lowercase 'test' at start
- Naming test functions without 'Test' prefix
- Using unrelated prefixes like 'Run' or 'Check'
Solution
Step 1: Recall Go test command
The command to run tests in Go isgo test.Step 2: Compare options
Only go test matches the correct command. Others are invalid commands.Final Answer:
go test -> Option DQuick Check:
Run tests with 'go test' [OK]
- Using 'go run test' instead of 'go test'
- Trying 'go build test' which builds but doesn't test
- Using non-existent commands like 'go execute test'
go test on this test file?package main
import "testing"
func TestSum(t *testing.T) {
if 2+2 != 4 {
t.Error("Sum failed")
}
}Solution
Step 1: Analyze the test logic
The test checks if 2+2 equals 4, which is true, so no error is called.Step 2: Understand test output
Since the test passes,go testoutputs PASS with timing info.Final Answer:
PASS ok main 0.001s -> Option CQuick Check:
Test passes, so output shows PASS [OK]
- Assuming syntax error due to missing main
- Expecting FAIL when condition is true
- Thinking tests won't run without main function
go test from running it:package main
import "testing"
func testAdd(t *testing.T) {
if 1+1 != 2 {
t.Error("Add failed")
}
}Solution
Step 1: Check test function naming
The function is namedtestAddwith lowercase 't', sogo testignores it.Step 2: Verify other requirements
Test functions must start with uppercase 'Test' and accept*testing.Tparameter, which is correct except the name.Final Answer:
Function name must start with uppercase 'Test' -> Option BQuick Check:
Test functions need 'Test' prefix [OK]
- Thinking test functions need to return a value
- Assuming missing 'fmt' import causes test failure
- Believing test functions cannot have parameters
math_test.go inside your module. Which command should you use?Solution
Step 1: Understand how to run tests in a specific file
Go allows specifying test files directly as arguments togo test.Step 2: Check options for correct usage
Onlygo test math_test.goruns tests in the specified file. B uses invalid-fileflag, C'sgo test -vruns all tests verbosely, D's-runmatches test names.Final Answer:
go test math_test.go -> Option AQuick Check:
Specify test file directly to run its tests [OK]
- Using invalid flags like -run or -file with filenames
- Confusing verbose flag -v with file selection
- Not specifying the file and running all tests
Ready to prove it?
Pass the test and this pattern moves unknown
