| 1 | <a id="top"></a> |
| 2 | # Explicitly skipping, passing, and failing tests at runtime |
| 3 | |
| 4 | ## Skipping Test Cases at Runtime |
| 5 | |
| 6 | > [Introduced](https://github.com/catchorg/Catch2/pull/2360) in Catch2 3.3.0. |
| 7 | |
| 8 | In some situations it may not be possible to meaningfully execute a test case, |
| 9 | for example when the system under test is missing certain hardware capabilities. |
| 10 | If the required conditions can only be determined at runtime, it often |
| 11 | doesn't make sense to consider such a test case as either passed or failed, |
| 12 | because it simply can not run at all. |
| 13 | |
| 14 | To properly express such scenarios, Catch2 provides a way to explicitly |
| 15 | _skip_ test cases, using the `SKIP` macro: |
| 16 | |
| 17 | ``` |
| 18 | SKIP( [streamable expression] ) |
| 19 | ``` |
| 20 | |
| 21 | Example usage: |
| 22 | |
| 23 | ```c++ |
| 24 | TEST_CASE("copy files between drives") { |
| 25 | if(getNumberOfHardDrives() < 2) { |
| 26 | SKIP("at least two hard drives required"); |
| 27 | } |
| 28 | // ... |
| 29 | } |
| 30 | ``` |
| 31 | |
| 32 | This test case is then reported as _skipped_ instead of _passed_ or _failed_. |
| 33 | |
| 34 | The `SKIP` macro behaves similarly to an explicit [`FAIL`](#passing-and-failing-test-cases), |
| 35 | in that it is the last expression that will be executed: |
| 36 | |
| 37 | ```c++ |
| 38 | TEST_CASE("my test") { |
| 39 | printf("foo"); |
| 40 | SKIP(); |
| 41 | printf("bar"); // not printed |
| 42 | } |
| 43 | ``` |
| 44 | |
| 45 | However a failed assertion _before_ a `SKIP` still causes the entire |
| 46 | test case to fail: |
| 47 | |
| 48 | ```c++ |
| 49 | TEST_CASE("failing test") { |
| 50 | CHECK(1 == 2); |
| 51 | SKIP(); |
| 52 | } |
| 53 | ``` |
| 54 | |
| 55 | ### Interaction with Sections and Generators |
| 56 | |
| 57 | Sections, nested sections as well as specific outputs from [generators](generators.md#top) |
| 58 | can all be individually skipped, with the rest executing as usual: |
| 59 | |
| 60 | ```c++ |
| 61 | TEST_CASE("complex test case") { |
| 62 | int value = GENERATE(2, 4, 6); |
| 63 | SECTION("a") { |
| 64 | SECTION("a1") { CHECK(value < 8); } |
| 65 | SECTION("a2") { |
| 66 | if (value == 4) { |
| 67 | SKIP(); |
| 68 | } |
| 69 | CHECK(value % 2 == 0); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | ``` |
| 74 | |
| 75 | This test case will report 5 passing assertions; one for each of the three |
| 76 | values in section `a1`, and then two in section `a2`, from values 2 and 4. |
| 77 | |
| 78 | Note that as soon as one section is skipped, the entire test case will |
| 79 | be reported as _skipped_ (unless there is a failing assertion, in which |
| 80 | case the test is handled as _failed_ instead). |
| 81 | |
| 82 | Note that if all test cases in a run are skipped, Catch2 returns a non-zero |
| 83 | exit code, same as it does if no test cases have run. This behaviour can |
| 84 | be overridden using the [--allow-running-no-tests](command-line.md#no-tests-override) |
| 85 | flag. |
| 86 | |
| 87 | ### `SKIP` inside generators |
| 88 | |
| 89 | You can also use the `SKIP` macro inside generator's constructor to handle |
| 90 | cases where the generator is empty, but you do not want to fail the test |
| 91 | case. |
| 92 | |
| 93 | |
| 94 | ## Passing and failing test cases |
| 95 | |
| 96 | Test cases can also be explicitly passed or failed, without the use of |
| 97 | assertions, and with a specific message. This can be useful to handle |
| 98 | complex preconditions/postconditions and give useful error messages |
| 99 | when they fail. |
| 100 | |
| 101 | * `SUCCEED( [streamable expression] )` |
| 102 | |
| 103 | `SUCCEED` is morally equivalent with `INFO( [streamable expression] ); REQUIRE( true );`. |
| 104 | Note that it does not stop further test execution, so it cannot be used |
| 105 | to guard failing assertions from being executed. |
| 106 | |
| 107 | _In practice, `SUCCEED` is usually used as a test placeholder, to avoid |
| 108 | [failing a test case due to missing assertions](command-line.md#warnings)._ |
| 109 | |
| 110 | ```cpp |
| 111 | TEST_CASE( "SUCCEED showcase" ) { |
| 112 | int I = 1; |
| 113 | SUCCEED( "I is " << I ); |
| 114 | // ... execution continues here ... |
| 115 | } |
| 116 | ``` |
| 117 | |
| 118 | * `FAIL( [streamable expression] )` |
| 119 | |
| 120 | `FAIL` is morally equivalent with `INFO( [streamable expression] ); REQUIRE( false );`. |
| 121 | |
| 122 | _In practice, `FAIL` is usually used to stop executing test that is currently |
| 123 | known to be broken, but has to be fixed later._ |
| 124 | |
| 125 | ```cpp |
| 126 | TEST_CASE( "FAIL showcase" ) { |
| 127 | FAIL( "This test case causes segfault, which breaks CI." ); |
| 128 | // ... this will not be executed ... |
| 129 | } |
| 130 | ``` |
| 131 | |
| 132 | |
| 133 | --- |
| 134 | |
| 135 | [Home](Readme.md#top) |