doctest logo

doctest

Free

A lightweight and feature-rich C++98/C++11 single-header testing framework for unit tests and TDD.

FreeFree tier
Type
Open Source

About doctest

doctest is a new C++ testing framework designed to be the fastest in both compile times and runtime, significantly outperforming other feature-rich alternatives. It provides a fast, transparent, and flexible test runner with a clean interface. A distinctive feature is the ability to write tests directly in production code, enabling tests to serve as documentation and up-to-date comments. The framework is ultra-light on compile times, produces no warnings even on aggressive warning levels, and can remove all testing-related code from the binary using the DOCTEST_CONFIG_DISABLE identifier. It is thread-safe, has no global namespace pollution, and does not drag any headers with it. Portable C++11 (with a tag for C++98) and over 100 different CI builds ensure reliability.

Key Features

Ultra-light on compile times (orders of magnitude faster than alternatives)
Single-header implementation
Self-registering tests that compile to an executable
Thread-safe asserts from multiple threads spawned from a single test case
No global namespace pollution (everything in doctest::)
Can remove all testing-related code from the binary with DOCTEST_CONFIG_DISABLE
Portable C++11 with tag for C++98 support
Over 100 different CI builds (static analysis, sanitizers)
Binaries (exe/dll) can share test runner registry
Tests can be written directly in production code (source or header files)

Pros & Cons

Pros
  • Fastest compile times among comparable C++ testing frameworks
  • Lightweight and minimal overhead
  • No compiler warnings even on highest warning levels
  • Thread-safe - asserts can be used from multiple threads
  • Portable across C++11, C++14, C++17, C++20, C++23 and C++98
  • Can completely disable testing from the binary for production builds
  • Allows tests to be embedded directly in production code
Cons
  • C++ only - not suitable for non-C++ projects
  • Less widespread adoption compared to catch or Google Test
  • Requires C++11 or later (C++98 via older tag only)
  • May have a learning curve for developers used to other frameworks

Best For

Unit testing and TDD in C++ projectsWriting tests directly in source or header files to keep tests near production codeTesting internal APIs not exposed in public headersDocumentation through tests (tests as up-to-date comments)General-purpose assertion library outside of testing contextMulti-threaded testing with thread-safe asserts

FAQ

What is doctest?
doctest is a fast, feature-rich C++ single-header testing framework that allows tests to be written directly in production code. It is designed for unit testing and TDD with extremely fast compile times.
How does doctest compare to Catch or Google Test?
doctest is modeled after Catch and shares some code, but it is significantly faster in compile times (by orders of magnitude) and runtime. It also offers thread-safe asserts, no global namespace pollution, and the unique ability to place tests directly in production code.
Can I use doctest with older C++ standards?
Yes, doctest is portable C++11 and also supports C++98 via the 1.2.9 tag. The main branch targets C++11/14/17/20/23.
Is doctest thread-safe?
Yes, asserts can be used from multiple threads spawned from a single test case, making it suitable for multi-threaded testing.
How can I remove doctest from a production binary?
Define the DOCTEST_CONFIG_DISABLE identifier to remove all testing-related code from the binary.