Testing: go test and CTest
C++ has no test runner in the language. A project either takes a framework such as GoogleTest, Catch2 or doctest, or writes the smallest harness it can and lets CTest, CMake's runner, start the binary. Go has the runner in the toolchain, the assertions in the standard library and the file naming in the language. Both programs below test the same two functions and both are expected to fail on the same case.
A harness and CTest
The library is two functions in a namespace, declared in a header. The test is a CHECK macro that prints the failing condition and a counter, and a main that returns non-zero when the counter is not zero; CMake registers that binary as one test.
#pragma once
namespace calc {
int add(int a, int b);
int divide(int a, int b);
}
#include "calc.h"
namespace calc {
int add(int a, int b)
{
return a + b;
}
int divide(int a, int b)
{
if (b == 0) {
return 0;
}
return a / b;
}
}
#include "calc.h"
#include <iostream>
static int failures = 0;
#define CHECK(condition) \
do { \
if (!(condition)) { \
std::cerr << "FAILED: " #condition " at line " << __LINE__ << "\n"; \
++failures; \
} \
} while (0)
int main()
{
CHECK(calc::add(2, 2) == 4);
CHECK(calc::divide(10, 2) == 5);
CHECK(calc::divide(1, 0) == -1);
return failures == 0 ? 0 : 1;
}
cmake_minimum_required(VERSION 3.20)
project(calc CXX)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_library(calc STATIC calc.cpp)
add_executable(test_calc test_calc.cpp)
target_link_libraries(test_calc PRIVATE calc)
target_compile_options(test_calc PRIVATE -Wall -Wextra)
enable_testing()
add_test(NAME calc COMMAND test_calc)
cmake -S . -B build
cmake --build build
[ 75%] Building CXX object CMakeFiles/test_calc.dir/test_calc.cpp.o
[100%] Linking CXX executable test_calc
[100%] Built target test_calc
The failure CTest prints
The third check expects minus one from a division by zero that returns zero. CTest sees a non-zero exit and prints what the binary wrote:
ctest --test-dir build --output-on-failure; echo "status: $?"
Test project /tmp/test/cpp/build
Start 1: calc
1/1 Test #1: calc .............................***Failed 0.01 sec
FAILED: calc::divide(1, 0) == -1 at line 19
0% tests passed, 1 tests failed out of 1
Total Test time (real) = 0.03 sec
The following tests FAILED:
1 - calc (Failed)
Errors while running CTest
status: 8
The runner reports one test, because the harness inside the binary counts three checks and reports them as one. A failure in the first check still runs the other two, so one run reports every broken expectation, and the exit status of CTest is 8 rather than 1.
The same tests with go test
The Go package is the two functions, and the test file is named after the package. There is no harness: go test finds every function whose name starts with Test, passes it a *testing.T, and the table gives each case a name of its own.
package calc
// Add returns a + b.
func Add(a, b int) int {
return a + b
}
// Divide returns a / b, or 0 when b is zero.
func Divide(a, b int) int {
if b == 0 {
return 0
}
return a / b
}
package calc
import "testing"
func TestAdd(t *testing.T) {
if got, want := Add(2, 2), 4; got != want {
t.Errorf("Add(2, 2) = %d, want %d", got, want)
}
}
func TestDivide(t *testing.T) {
cases := []struct {
name string
a, b int
want int
}{
{"even", 10, 2, 5},
{"by zero", 1, 0, -1},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := Divide(tc.a, tc.b); got != tc.want {
t.Errorf("Divide(%d, %d) = %d, want %d", tc.a, tc.b, got, tc.want)
}
})
}
}
go test -v .; echo "status: $?"
=== RUN TestAdd
--- PASS: TestAdd (0.00s)
=== RUN TestDivide
=== RUN TestDivide/even
=== RUN TestDivide/by_zero
calc_test.go:23: Divide(1, 0) = 0, want -1
--- FAIL: TestDivide (0.00s)
--- PASS: TestDivide/even (0.00s)
--- FAIL: TestDivide/by_zero (0.00s)
FAIL
FAIL example.com/calc 0.005s
FAIL
status: 1
The failing case is a subtest with its own name, and the passing cases are listed beside it. The exit status is 1, the file and line come from the framework, and the message between them is the sentence the test author wrote. To run only the broken case:
go test -v -run TestDivide/by_zero .
=== RUN TestDivide
=== RUN TestDivide/by_zero
calc_test.go:23: Divide(1, 0) = 0, want -1
--- FAIL: TestDivide (0.00s)
--- FAIL: TestDivide/by_zero (0.00s)
FAIL
FAIL example.com/calc 0.004s
FAIL
The check that disappears
The C++ standard library has an assertion that the preprocessor removes. With -DNDEBUG, the usual flag of a release build, the check is gone and this program reports success with a wrong expectation in its source:
#include <cassert>
#include <iostream>
int add(int a, int b)
{
return a + b;
}
int main()
{
assert(add(2, 2) == 5);
std::cout << "all tests passed\n";
}
g++ -std=c++23 -O2 -Wall -Wextra -DNDEBUG assert_off.cpp -o assert_off
./assert_off; echo "status: $?"
all tests passed
status: 0
Go has no such flag. A check written with if is in every build, and the testing package is compiled into the test binary, not into the program, so a release build cannot drop it:
package main
import "fmt"
func add(a, b int) int {
return a + b
}
func main() {
if add(2, 2) != 5 {
panic("add(2, 2) != 5")
}
fmt.Println("all tests passed")
}
go run -trimpath ./panic; echo "status: $?"
panic: add(2, 2) != 5
goroutine 1 [running]:
main.main()
example.com/calc/panic/main.go:11 +0x25
exit status 2
status: 1
The point is not that a panic is a good test framework; it is that the Go compiler offers no switch that removes an ordinary if. A program that must fail loudly in production uses a check, and a program that wants a cheap assertion in a hot loop uses a build tag or a constant, which are visible in the source rather than in a compiler flag.
What differs and why
| Question | C++ | Go |
|---|---|---|
| Where do tests live? | Anywhere the build lists them | In _test.go files of the package |
| Who finds a test? | The runner reads a CMake list, or the framework registers it | The toolchain finds the functions |
| What does a subtest cost? | Another add_test and another process | A t.Run call in the same process |
| Can a check vanish in a release build? | Yes, with NDEBUG | No |
| What does the runner add? | Report, timeouts, labels, parallel jobs: CTest or the framework | The same subset, plus the race detector and coverage |
C++ testing is not worse, it is assembled: GoogleTest gives expectations, fixtures and death tests that Go does not have, and a project that already uses it has no reason to change. Go testing is smaller and uniform, and every Go project has it on the first day, which is the property that matters for a codebase maintained by people who come and go.
The Go lessons are go test and test files and table tests and subtests; the group Testing Go code goes further into the command, the libraries and the fuzzers. The C++ lesson is unit testing without a framework. The previous page of this group is build and dependencies.
The C++ project was built with g++ 15.2.0 (Alpine 15.2.0) through CMake 4.2.3 and CTest, and the Go programs with go 1.26.10, all on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev