Martin Hořeňovský | 1d9b506 | 2018-09-17 10:51:44 +0200 | [diff] [blame] | 1 | <a id="top"></a> |
| 2 | # Other macros |
| 3 | |
| 4 | This page serves as a reference for macros that are not documented |
| 5 | elsewhere. For now, these macros are separated into 2 rough categories, |
| 6 | "assertion related macros" and "test case related macros". |
| 7 | |
| 8 | ## Assertion related macros |
| 9 | |
| 10 | * `CHECKED_IF` and `CHECKED_ELSE` |
| 11 | |
| 12 | `CHECKED_IF( expr )` is an `if` replacement, that also applies Catch2's |
| 13 | stringification machinery to the _expr_ and records the result. As with |
| 14 | `if`, the block after a `CHECKED_IF` is entered only if the expression |
| 15 | evaluates to `true`. `CHECKED_ELSE( expr )` work similarly, but the block |
| 16 | is entered only if the _expr_ evaluated to `false`. |
| 17 | |
| 18 | Example: |
| 19 | ```cpp |
| 20 | int a = ...; |
| 21 | int b = ...; |
| 22 | CHECKED_IF( a == b ) { |
| 23 | // This block is entered when a == b |
| 24 | } CHECKED_ELSE ( a == b ) { |
| 25 | // This block is entered when a != b |
| 26 | } |
| 27 | ``` |
| 28 | |
| 29 | * `CHECK_NOFAIL` |
| 30 | |
| 31 | `CHECK_NOFAIL( expr )` is a variant of `CHECK` that does not fail the test |
| 32 | case if _expr_ evaluates to `false`. This can be useful for checking some |
Ryan Pavlik | edde6f4 | 2019-04-08 16:30:28 -0500 | [diff] [blame] | 33 | assumption, that might be violated without the test necessarily failing. |
Martin Hořeňovský | 1d9b506 | 2018-09-17 10:51:44 +0200 | [diff] [blame] | 34 | |
| 35 | Example output: |
| 36 | ``` |
| 37 | main.cpp:6: |
| 38 | FAILED - but was ok: |
| 39 | CHECK_NOFAIL( 1 == 2 ) |
| 40 | |
| 41 | main.cpp:7: |
| 42 | PASSED: |
| 43 | CHECK( 2 == 2 ) |
| 44 | ``` |
| 45 | |
| 46 | * `SUCCEED` |
| 47 | |
| 48 | `SUCCEED( msg )` is mostly equivalent with `INFO( msg ); REQUIRE( true );`. |
| 49 | In other words, `SUCCEED` is for cases where just reaching a certain line |
| 50 | means that the test has been a success. |
| 51 | |
| 52 | Example usage: |
| 53 | ```cpp |
| 54 | TEST_CASE( "SUCCEED showcase" ) { |
| 55 | int I = 1; |
| 56 | SUCCEED( "I is " << I ); |
| 57 | } |
| 58 | ``` |
| 59 | |
| 60 | * `STATIC_REQUIRE` |
| 61 | |
Clare Macrae | 49cd7c9 | 2019-07-22 12:49:08 +0100 | [diff] [blame] | 62 | > [Introduced](https://github.com/catchorg/Catch2/issues/1362) in Catch 2.4.2. |
| 63 | |
Martin Hořeňovský | 1d9b506 | 2018-09-17 10:51:44 +0200 | [diff] [blame] | 64 | `STATIC_REQUIRE( expr )` is a macro that can be used the same way as a |
| 65 | `static_assert`, but also registers the success with Catch2, so it is |
| 66 | reported as a success at runtime. The whole check can also be deferred |
| 67 | to the runtime, by defining `CATCH_CONFIG_RUNTIME_STATIC_REQUIRE` before |
| 68 | including the Catch2 header. |
| 69 | |
| 70 | Example: |
| 71 | ```cpp |
| 72 | TEST_CASE("STATIC_REQUIRE showcase", "[traits]") { |
| 73 | STATIC_REQUIRE( std::is_void<void>::value ); |
| 74 | STATIC_REQUIRE_FALSE( std::is_void<int>::value ); |
| 75 | } |
| 76 | ``` |
| 77 | |
| 78 | ## Test case related macros |
| 79 | |
| 80 | * `METHOD_AS_TEST_CASE` |
| 81 | |
| 82 | `METHOD_AS_TEST_CASE( member-function-pointer, description )` lets you |
| 83 | register a member function of a class as a Catch2 test case. The class |
| 84 | will be separately instantiated for each method registered in this way. |
| 85 | |
| 86 | ```cpp |
| 87 | class TestClass { |
| 88 | std::string s; |
| 89 | |
| 90 | public: |
| 91 | TestClass() |
| 92 | :s( "hello" ) |
| 93 | {} |
| 94 | |
| 95 | void testCase() { |
| 96 | REQUIRE( s == "hello" ); |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | |
| 101 | METHOD_AS_TEST_CASE( TestClass::testCase, "Use class's method as a test case", "[class]" ) |
| 102 | ``` |
| 103 | |
| 104 | * `REGISTER_TEST_CASE` |
| 105 | |
| 106 | `REGISTER_TEST_CASE( function, description )` let's you register |
| 107 | a `function` as a test case. The function has to have `void()` signature, |
| 108 | the description can contain both name and tags. |
| 109 | |
| 110 | Example: |
| 111 | ```cpp |
| 112 | REGISTER_TEST_CASE( someFunction, "ManuallyRegistered", "[tags]" ); |
| 113 | ``` |
| 114 | |
| 115 | _Note that the registration still has to happen before Catch2's session |
| 116 | is initiated. This means that it either needs to be done in a global |
| 117 | constructor, or before Catch2's session is created in user's own main._ |
| 118 | |
| 119 | |
| 120 | * `ANON_TEST_CASE` |
| 121 | |
| 122 | `ANON_TEST_CASE` is a `TEST_CASE` replacement that will autogenerate |
| 123 | unique name. The advantage of this is that you do not have to think |
| 124 | of a name for the test case,`the disadvantage is that the name doesn't |
Ryan Pavlik | edde6f4 | 2019-04-08 16:30:28 -0500 | [diff] [blame] | 125 | necessarily remain stable across different links, and thus it might be |
Martin Hořeňovský | 1d9b506 | 2018-09-17 10:51:44 +0200 | [diff] [blame] | 126 | hard to run directly. |
| 127 | |
| 128 | Example: |
| 129 | ```cpp |
| 130 | ANON_TEST_CASE() { |
| 131 | SUCCEED("Hello from anonymous test case"); |
| 132 | } |
| 133 | ``` |
| 134 | |
| 135 | * `DYNAMIC_SECTION` |
| 136 | |
Clare Macrae | 67b4ada | 2019-07-22 13:29:56 +0100 | [diff] [blame] | 137 | > Introduced in Catch 2.3.0. |
| 138 | |
Martin Hořeňovský | 1d9b506 | 2018-09-17 10:51:44 +0200 | [diff] [blame] | 139 | `DYNAMIC_SECTION` is a `SECTION` where the user can use `operator<<` to |
| 140 | create the final name for that section. This can be useful with e.g. |
| 141 | generators, or when creating a `SECTION` dynamically, within a loop. |
| 142 | |
| 143 | Example: |
| 144 | ```cpp |
| 145 | TEST_CASE( "looped SECTION tests" ) { |
| 146 | int a = 1; |
| 147 | |
| 148 | for( int b = 0; b < 10; ++b ) { |
| 149 | DYNAMIC_SECTION( "b is currently: " << b ) { |
| 150 | CHECK( b > a ); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | ``` |