doctest

FORK: The fastest feature-rich C++11/14/17/20 single-header testing framework
git clone https://git.neptards.moe/neptards/doctest.git
Log | Files | Refs | README

configuration.md (19207B)


      1 ## Configuration
      2 
      3 **doctest** is designed to "just work" as much as possible. It also allows configuring how it is built with a set of identifiers.
      4 
      5 The identifiers should be defined before the inclusion of the framework header.
      6 
      7 Defining something ```globally``` means for every source file of the binary (executable / shared object).
      8 
      9 - [**```DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN```**](#doctest_config_implement_with_main)
     10 - [**```DOCTEST_CONFIG_IMPLEMENT```**](#doctest_config_implement)
     11 - [**```DOCTEST_CONFIG_DISABLE```**](#doctest_config_disable)
     12 - [**```DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL```**](#doctest_config_implementation_in_dll)
     13 - [**```DOCTEST_CONFIG_NO_SHORT_MACRO_NAMES```**](#doctest_config_no_short_macro_names)
     14 - [**```DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING```**](#doctest_config_treat_char_star_as_string)
     15 - [**```DOCTEST_CONFIG_REQUIRE_STRINGIFICATION_FOR_ALL_USED_TYPES```**](#doctest_config_require_stringification_for_all_used_types)
     16 - [**```DOCTEST_CONFIG_DOUBLE_STRINGIFY```**](#doctest_config_double_stringify)
     17 - [**```DOCTEST_CONFIG_SUPER_FAST_ASSERTS```**](#doctest_config_super_fast_asserts)
     18 - [**```DOCTEST_CONFIG_USE_STD_HEADERS```**](#doctest_config_use_std_headers)
     19 - [**```DOCTEST_CONFIG_VOID_CAST_EXPRESSIONS```**](#doctest_config_void_cast_expressions)
     20 - [**```DOCTEST_CONFIG_NO_COMPARISON_WARNING_SUPPRESSION```**](#doctest_config_no_comparison_warning_suppression)
     21 - [**```DOCTEST_CONFIG_OPTIONS_PREFIX```**](#doctest_config_options_prefix)
     22 - [**```DOCTEST_CONFIG_NO_UNPREFIXED_OPTIONS```**](#doctest_config_no_unprefixed_options)
     23 - [**```DOCTEST_CONFIG_NO_TRY_CATCH_IN_ASSERTS```**](#doctest_config_no_try_catch_in_asserts)
     24 - [**```DOCTEST_CONFIG_NO_EXCEPTIONS```**](#doctest_config_no_exceptions)
     25 - [**```DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS```**](#doctest_config_no_exceptions_but_with_all_asserts)
     26 - [**```DOCTEST_CONFIG_ASSERTION_PARAMETERS_BY_VALUE```**](#doctest_config_assertion_parameters_by_value)
     27 - [**```DOCTEST_CONFIG_COLORS_NONE```**](#doctest_config_colors_none)
     28 - [**```DOCTEST_CONFIG_COLORS_WINDOWS```**](#doctest_config_colors_windows)
     29 - [**```DOCTEST_CONFIG_COLORS_ANSI```**](#doctest_config_colors_ansi)
     30 - [**```DOCTEST_CONFIG_WINDOWS_SEH```**](#doctest_config_windows_seh)
     31 - [**```DOCTEST_CONFIG_NO_WINDOWS_SEH```**](#doctest_config_no_windows_seh)
     32 - [**```DOCTEST_CONFIG_POSIX_SIGNALS```**](#doctest_config_posix_signals)
     33 - [**```DOCTEST_CONFIG_NO_POSIX_SIGNALS```**](#doctest_config_no_posix_signals)
     34 - [**```DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS```**](#doctest_config_include_type_traits)
     35 - [**```DOCTEST_CONFIG_NO_MULTITHREADING```**](#doctest_config_no_multithreading)
     36 - [**```DOCTEST_CONFIG_NO_MULTI_LANE_ATOMICS```**](#doctest_config_no_multi_lane_atomics)
     37 - [**```DOCTEST_CONFIG_ASSERTS_RETURN_VALUES```**](#doctest_config_asserts_return_values)
     38 - [**```DOCTEST_CONFIG_EVALUATE_ASSERTS_EVEN_WHEN_DISABLED```**](#doctest_config_evaluate_asserts_even_when_disabled)
     39 
     40 For most people the only configuration needed is telling **doctest** which source file should host all the implementation code:
     41 
     42 ### **```DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN```**
     43 
     44 ```c++
     45 #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
     46 #include "doctest.h"
     47 ```
     48 
     49 This should be defined only in the source file where the library is implemented. It also creates a ```main()``` entry point.
     50 
     51 ### **```DOCTEST_CONFIG_IMPLEMENT```**
     52 
     53 If the client wants to [**supply the ```main()``` function**](main.md) (either to set an option with some value from the code or to integrate the framework into their existing project codebase) this identifier should be used.
     54 
     55 This should be defined only in the source file where the library is implemented.
     56 
     57 ### **```DOCTEST_CONFIG_DISABLE```**
     58 
     59 One of the most important configuration options - everything testing-related is removed from the binary - including most of the framework implementation and every test case written anywhere! This is one of the most unique features of **doctest**.
     60 
     61 This should be defined globally.
     62 
     63 ### **```DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL```**
     64 
     65 This will affect the public interface of doctest - all necessary forward declarations for writing tests will be turned into imported symbols. That way the test runner doesn't have to be implemented in the binary (executable / shared object) and can be reused from another binary where it is built and exported.
     66 
     67 To export the test runner from a binary simply use [**```DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL```**](#doctest_config_implementation_in_dll) together with [**```DOCTEST_CONFIG_IMPLEMENT```**](#doctest_config_implement) (or [**```DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN```**](#doctest_config_implement_with_main) but then the other binaries will have to link to the executable) in whatever source file the test runner gets implemented into. Note that this identifier should not be defined in the other source files of the binary which exports the doctest test runner - or there will be linker conflicts - having the same symbols as both imported and exported within the same binary.
     68 
     69 Checkout the [**example**](../../examples/executable_dll_and_plugin/) - it shows how to have the test runner implemented in a dll (and there are even tests in a plugin which is dynamically loaded).
     70 
     71 This should be defined globally in binaries that import the symbols.
     72 
     73 This should be defined only in the source file where the library is implemented for binaries that export the test runner.
     74 
     75 ### **```DOCTEST_CONFIG_NO_SHORT_MACRO_NAMES```**
     76 
     77 This will remove all macros from **doctest** that don't have the **```DOCTEST_```** prefix - like **```CHECK```**, **```TEST_CASE```** and **```SUBCASE```**. Then only the full macro names will be available - **```DOCTEST_CHECK```**, **```DOCTEST_TEST_CASE```** and **```DOCTEST_SUBCASE```**. The user is free to make their own short versions of these macros - [**example**](../../examples/all_features/alternative_macros.cpp).
     78 
     79 This can be defined both globally and in specific source files only.
     80 
     81 ### **```DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING```**
     82 
     83 By default ```char*``` is being treated as a pointer. With this option comparing ```char*``` pointers will switch to using ```strcmp()``` for comparisons and when stringified the string will be printed instead of the pointer value.
     84 
     85 This should be defined globally.
     86 
     87 ### **```DOCTEST_CONFIG_REQUIRE_STRINGIFICATION_FOR_ALL_USED_TYPES```**
     88 
     89 By default if stringification is not available for a type, it is simply printed as `{?}`. By enabling this flag, whenever a type is used in an assert that does not provide stringification, the compilation is stopped.
     90 
     91 This can be defined both globally and in specific source files only.
     92 
     93 ### **```DOCTEST_CONFIG_DOUBLE_STRINGIFY```**
     94 
     95 If you define your own `toString` functions that return something, that's different from `doctest::String`, but still stringifiable, you can enable this flag to stringify the result of internal stringification calls again.
     96 
     97 You can also define `DOCTEST_STRINGIFY` yourself to override doctest's stringification behavior.
     98 
     99 This can be defined both globally and in specific source files only.
    100 
    101 ### **```DOCTEST_CONFIG_SUPER_FAST_ASSERTS```**
    102 
    103 This config option makes the assert macros (except for those dealing with exceptions) compile [**much faster**](benchmarks.md#cost-of-an-assertion-macro)! (31-91% - depending on the type - [**normal**](assertions.md#expression-decomposing-asserts) or [**binary**](assertions.md#binary-and-unary-asserts))
    104 
    105 Each assert is turned into a single function call - the only downside of this is: if an assert fails and a debugger is attached - when it breaks it will be in an internal function - the user will have to go 1 level up in the callstack to see the actual assert.
    106 
    107 It also implies [**```DOCTEST_CONFIG_NO_TRY_CATCH_IN_ASSERTS```**](#doctest_config_no_try_catch_in_asserts) (so exceptions thrown during the evaluation of an assert are not caught by the assert itself but by the testing framework - meaning that the test case is immediately aborted).
    108 
    109 This can be defined both globally and in specific source files only.
    110 
    111 ### **```DOCTEST_CONFIG_USE_STD_HEADERS```**
    112 
    113 The library by default provides a forward declaration of ```std::ostream``` in order to support the ```operator<<``` [**stringification**](stringification.md) mechanism (also ```std::tuple<>``` and ```std::nullptr_t```). This is forbidden by the standard (even though it works everywhere on all tested compilers). However if the user wishes to be 100% standards compliant - then this configuration option can be used to force the inclusion of the relevant standard headers.
    114 
    115 Also it is possible that some STL implementation of a compiler with niche usage defines them differently - then there will be compilation errors in STL headers and using this option should fix the problem.
    116 
    117 This should be defined globally.
    118 
    119 ### **```DOCTEST_CONFIG_VOID_CAST_EXPRESSIONS```**
    120 
    121 This affects the [asserts dealing with exceptions](assertions.md#exceptions) - the expression is cast to void to avoid problems such as when functions with the ```[[nodiscard]]``` attribute are used but their result isn't checked.
    122 
    123 This can be defined both globally and in specific source files only.
    124 
    125 ### **```DOCTEST_CONFIG_NO_COMPARISON_WARNING_SUPPRESSION```**
    126 
    127 By default the library suppresses warnings about comparing signed and unsigned types, etc.
    128 
    129 - g++/clang ```-Wsign-conversion```
    130 - g++/clang ```-Wsign-compare```
    131 - msvc ```C4389``` 'operator' : signed/unsigned mismatch
    132 - msvc ```C4018``` 'expression' : signed/unsigned mismatch
    133 
    134 You can checkout [**this**](https://github.com/doctest/doctest/issues/16#issuecomment-246803303) issue to better understand why I suppress these warnings by default.
    135 
    136 This can be defined both globally and in specific source files only.
    137 
    138 ### **```DOCTEST_CONFIG_OPTIONS_PREFIX```**
    139 
    140 Defining this as a string will change the prefix of the [**command line**](commandline.md) options to use the given prefix instead of the default ```dt-``` prefix. This can be useful for integrating the testing framework into a client codebase, where a command option prefix like ```selftest-``` might be more clear to users.
    141 
    142 This should be defined only in the source file where the library is implemented (it's relevant only there).
    143 
    144 ### **```DOCTEST_CONFIG_NO_UNPREFIXED_OPTIONS```**
    145 
    146 This will disable the short versions of the [**command line**](commandline.md) options and only the versions with ```--dt-``` prefix will be parsed by **doctest** - this is possible for easy interoperability with client command line option handling when the testing framework is integrated within a client codebase - so there are no clashes and so that the user can exclude everything starting with ```--dt-``` from their option parsing.
    147 
    148 This should be defined only in the source file where the library is implemented (it's relevant only there).
    149 
    150 ### **```DOCTEST_CONFIG_NO_TRY_CATCH_IN_ASSERTS```**
    151 
    152 This will remove all ```try``` / ```catch``` sections from:
    153 
    154 - the [normal asserts](assertions.md#expression-decomposing-asserts)
    155 - the [binary and unary asserts](assertions.md#binary-and-unary-asserts)
    156 
    157 so exceptions thrown while evaluating the expression in an assert will terminate the current test case.
    158 
    159 This can be used for some mild compile time savings but for greater impact look into [**```DOCTEST_CONFIG_SUPER_FAST_ASSERTS```**](configuration.md#doctest_config_super_fast_asserts).
    160 
    161 This can be defined both globally and in specific source files only.
    162 
    163 ### **```DOCTEST_CONFIG_NO_EXCEPTIONS```**
    164 
    165 This will remove everything that uses exceptions from the framework - it is also auto detectable if exceptions are disabled for compilers (like with ```-fno-exceptions``` for GCC/Clang).
    166 
    167 What gets changed:
    168 
    169 - asserts that evaluate the expression in a ```try``` / ```catch``` section no longer evaluate in such a context
    170 - ```REQUIRE``` macros are gone (undefined)
    171 - [exception macros](assertions.md#exceptions) are gone (undefined)
    172 - the ```abort-after``` option won't be fully working because an exception is used to terminate test cases
    173 
    174 The ```REQUIRE``` family of asserts uses exceptions to terminate the current test case when they fail. An exception is used instead of a simple ```return;``` because asserts can be used not only in a test case but also in functions called by a test case.
    175 
    176 Also some of the [**logging macros**](logging.md#messages-which-can-optionally-fail-test-cases) which act like a ```REQUIRE``` assert (terminating the test case) - like ```FAIL()``` - start to work differently - like a ```FAIL_CHECK()```.
    177 
    178 [**```DOCTEST_CONFIG_NO_EXCEPTIONS```**](#doctest_config_no_exceptions) implies [**```DOCTEST_CONFIG_NO_TRY_CATCH_IN_ASSERTS```**](#doctest_config_no_try_catch_in_asserts)
    179 
    180 If you wish to use asserts that deal with exceptions and only sometimes build without exceptions - check the [**```DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS```**](#doctest_config_no_exceptions_but_with_all_asserts) config option.
    181 
    182 This should be defined globally.
    183 
    184 ### **```DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS```**
    185 
    186 When building with no exceptions (see [**```DOCTEST_CONFIG_NO_EXCEPTIONS```**](#doctest_config_no_exceptions)) ```REQUIRE``` asserts and the ones about dealing with exceptions are gone.
    187 
    188 If however you want your code to use these assertions and only sometimes build without exceptions - then using this config will be of help. The effects of using it are the following:
    189 
    190 - ```REQUIRE``` asserts are not gone - but they act like ```CHECK``` asserts - when one of them fails the whole test case will be marked as failed but will not be exited immediately
    191 - the [asserts for dealing with exceptions](assertions.md#exceptions) are turned into a no-op (instead of being totally undefined)
    192 
    193 This can be defined both globally and in specific source files only.
    194 
    195 ### **```DOCTEST_CONFIG_ASSERTION_PARAMETERS_BY_VALUE```**
    196 
    197 This option forces all doctest asserts to copy by value the expressions they are given instead of binding them to const references. This might be useful to avoid ODR-usage of static constants (which might lead to linker errors with g++/clang):
    198 
    199 ```c++
    200 template<typename T> struct type_traits { static const bool value = false; };
    201 
    202 // unless DOCTEST_CONFIG_ASSERTION_PARAMETERS_BY_VALUE is defined the following assertion
    203 // will lead to a linker error if type_traits<int>::value isn't defined in a translation unit
    204 CHECK(type_traits<int>::value == false);
    205 ```
    206 
    207 This can be defined both globally and in specific source files only.
    208 
    209 ### **```DOCTEST_CONFIG_COLORS_NONE```**
    210 
    211 This will remove support for colors in the console output of the framework.
    212 
    213 This should be defined only in the source file where the library is implemented (it's relevant only there).
    214 
    215 ### **```DOCTEST_CONFIG_COLORS_WINDOWS```**
    216 
    217 This will force the support for colors in the console output to use the Windows APIs and headers.
    218 
    219 This should be defined only in the source file where the library is implemented (it's relevant only there).
    220 
    221 ### **```DOCTEST_CONFIG_COLORS_ANSI```**
    222 
    223 This will force the support for colors in the console output to use ANSI escape codes.
    224 
    225 This should be defined only in the source file where the library is implemented (it's relevant only there).
    226 
    227 ### **```DOCTEST_CONFIG_WINDOWS_SEH```**
    228 
    229 This will enable SEH handling on Windows. Currently enabled only when compiled with MSVC, because some versions of MinGW do not have the necessary Win32 API support. The user may choose to enable this explicitly - it is known to work with the MinGW-w64 project.
    230 
    231 This should be defined only in the source file where the library is implemented (it's relevant only there).
    232 
    233 ### **```DOCTEST_CONFIG_NO_WINDOWS_SEH```**
    234 
    235 This can be used to disable [**```DOCTEST_CONFIG_WINDOWS_SEH```**](#doctest_config_windows_seh) when it is auto-selected by the library.
    236 
    237 This should be defined only in the source file where the library is implemented (it's relevant only there).
    238 
    239 ### **```DOCTEST_CONFIG_POSIX_SIGNALS```**
    240 
    241 This will enable the use of signals under UNIX for handling crashes. On by default.
    242 
    243 This should be defined only in the source file where the library is implemented (it's relevant only there).
    244 
    245 ### **```DOCTEST_CONFIG_NO_POSIX_SIGNALS```**
    246 
    247 This can be used to disable **```DOCTEST_CONFIG_POSIX_SIGNALS```** when it is auto-selected by the library.
    248 
    249 This should be defined only in the source file where the library is implemented (it's relevant only there).
    250 
    251 ### **```DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS```**
    252 
    253 This can be used to include the ```<type_traits>``` C++11 header. That in turn will enable the ability for the ```Approx``` helper to be used with strong typedefs of ```double``` - check [this](https://github.com/doctest/doctest/issues/62) or [this](https://github.com/doctest/doctest/issues/85) issue for more details on that.
    254 
    255 This can be defined both globally and in specific source files only.
    256 
    257 ### **```DOCTEST_CONFIG_NO_MULTITHREADING```**
    258 
    259 This can be used to disable all multithreading support.
    260 
    261 Speeds up single threaded applications.
    262 
    263 Includes [**```DOCTEST_CONFIG_NO_MULTI_LANE_ATOMICS```**](#doctest_config_no_multi_lane_atomics).
    264 
    265 This should be defined only in the source file where the library is implemented (it's relevant only there).
    266 
    267 ### **```DOCTEST_CONFIG_NO_MULTI_LANE_ATOMICS```**
    268 
    269 This can be used to disable multi lane atomics. Multi lane atomics can speed up highly parallel use of assert statements, but have a small overhead for single threaded applications.
    270 
    271 This should be defined only in the source file where the library is implemented (it's relevant only there).
    272 
    273 ### **```DOCTEST_CONFIG_ASSERTS_RETURN_VALUES```**
    274 
    275 Makes all assertion macros return a boolean value, reporting whether the assertion succeeded. This can be used, for example, to have ```nullptr``` checks that don't terminate the test case on failure.
    276 
    277 Example:
    278 ```c++
    279 if (CHECK(somePtr != nullptr))
    280     CHECK(somePtr->someMethod() == 42);
    281 ```
    282 
    283 This has a slight negative impact on performance as well as disabling some functionality inside assertions (e.g. ```co_return```).
    284 
    285 When [**```DOCTEST_CONFIG_DISABLE```**](#doctest_config_disable) is defined, all macros return ```false``` by default.
    286 
    287 This can be defined both globally and in specific source files only.
    288 
    289 ### **```DOCTEST_CONFIG_EVALUATE_ASSERTS_EVEN_WHEN_DISABLED```**
    290 
    291 When [**```DOCTEST_CONFIG_ASSERTS_RETURN_VALUES```**](#doctest_config_asserts_return_values) and [**```DOCTEST_CONFIG_DISABLE```**](#doctest_config_disable) are defined, this macro will cause conditions from assertions to evaluate properly (instead of returning `false`), although all overhead and functionality from doctest is removed. This is useful when assertions are used in production code within if statements so that the condition continues to be evaluated.
    292 
    293 Since all ```THROWS_WITH``` assertions depend on doctest functionality which is not available when [**```DOCTEST_CONFIG_DISABLE```**](#doctest_config_disable) is defined (stringification), they will still unconditionally return ```false```.
    294 
    295 This can be defined both globally and in specific source files only.
    296 
    297 ---------------
    298 
    299 [Home](readme.md#reference)
    300 
    301 <p align="center"><img src="../../scripts/data/logo/icon_2.svg"></p>