Declare a test
#GoogleTest TEST defines a suite/test pair. Link an installed GoogleTest target and provide a test main or gtest_main.
Fragment
Reference
Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.
TypeScript/Node, Go 1.23+, Rust 2024 and C++23: from a first CLI to files, JSON, HTTP, concurrency and testing. Complete programs are labeled; other examples identify their surrounding code/imports. Optional libraries and version requirements are named in each entry.
Node’s built-in runner for JavaScript/compiled TypeScript, Go testing, Rust cargo test, and optional GoogleTest for C++. Excerpts assume a test body and named application functions unless marked as complete.
C++ docs GoogleTest user guide ↗ctest ↗
GoogleTest TEST defines a suite/test pair. Link an installed GoogleTest target and provide a test main or gtest_main.
Fragment
CTest runs tests registered by your CMake project. Building alone does not execute them.
Shell: cmake --build build; ctest --test-dir build --output-on-failure. Register with include(CTest) and gtest_discover_tests after finding GoogleTest.
GoogleTest supports suite/test filters; CTest also offers its own registered-test regex filter.
Shell: ./build/tests --gtest_filter=DoubleTest.*; ctest --test-dir build -R DoubleTest --output-on-failure
GoogleTest parameterized tests report separate instantiations. TEST_P, fixture declaration and instantiation work together.
Fragment
EXPECT_EQ uses equality and keeps the test running. ASSERT_EQ aborts the current test function on failure.
Fragment
EXPECT_NEAR uses an absolute error bound. Choose it for the scale of the value; EXPECT_DOUBLE_EQ follows a different floating-point comparison contract.
Fragment
GoogleTest can check thrown exception types. Error-code/expected-return APIs need assertions on that return value instead.
Fragment
A useful failure says which input failed, what was observed and what was expected. Assert public behavior; avoid pinning incidental private details that are free to change.
Fragment
RAII guards release owned resources when leaving scope, including assertion-induced returns. Fixtures can supply SetUp/TearDown for repeated setup.
Fragment
Include memory; Resource is an application-defined RAII type.
Join/await the owned computation before the test ends. Future get propagates the stored exception to the caller.
Fragment
Include future; load is an application function.
wait_for can bound waiting on a future, but does not cancel its task. A future from async may still wait at destruction; use cooperative stop and owned joining.
Fragment
This checks readiness only; it is not a cancellation implementation.
Use temporary directories, unique database names/keys, random free ports and explicit clocks/configuration. Restore global state in cleanup when isolation is impossible. Tests that run in parallel must not mutate the same process-wide state unsafely.
Fragment
A stub supplies answers, a fake implements simplified behavior and a mock verifies chosen interactions. Keep essential protocol semantics in the double; separately test the real adapter against the real dependency.
Fragment
Inject a clock/scheduler or use the runner’s documented fake-time facilities. Advance to an observable condition; sleeping a guessed duration creates slow and flaky tests. Real I/O and other threads may not use the fake clock.
Fragment
Cover ordinary input, empty/min/max values, malformed input, duplicates, missing values and meaningful ordering. Derive cases from the contract rather than repeating the implementation’s branches.
Fragment
Property testing checks a relationship over generated inputs and often shrinks a failure. The language’s test runner alone does not always supply a property framework. Define input constraints and preserve the failing seed/example.
Fragment
Coverage-guided fuzzers such as libFuzzer require an explicit target and compatible compiler instrumentation. GoogleTest alone is not a fuzzer.
LLVM libFuzzer: https://llvm.org/docs/LibFuzzer.html
Start the actual dependency with a known configuration, create isolated data, exercise one external contract and tear it down. Include serialization, error mapping, timeouts and authorization where they belong to that boundary.
Fragment
Coverage shows what ran under a selected instrumentation model. A high percentage does not prove correct assertions, all input combinations or absence of races. Inspect missing meaningful behavior rather than forcing a target number.
Fragment