| 1 | |
| 2 | // Copyright Catch2 Authors |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // (See accompanying file LICENSE.txt or copy at |
| 5 | // https://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | |
| 9 | #include <catch2/catch_test_macros.hpp> |
| 10 | #include <catch2/generators/catch_generators_range.hpp> |
| 11 | |
| 12 | #include <iostream> |
| 13 | |
| 14 | TEST_CASE( "tests can be skipped dynamically at runtime", "[skipping]" ) { |
| 15 | SKIP(); |
| 16 | FAIL( "this is not reached" ); |
| 17 | } |
| 18 | |
| 19 | TEST_CASE( "skipped tests can optionally provide a reason", "[skipping]" ) { |
| 20 | const int answer = 43; |
| 21 | SKIP( "skipping because answer = " << answer ); |
| 22 | FAIL( "this is not reached" ); |
| 23 | } |
| 24 | |
| 25 | TEST_CASE( "sections can be skipped dynamically at runtime", "[skipping]" ) { |
| 26 | SECTION( "not skipped" ) { SUCCEED(); } |
| 27 | SECTION( "skipped" ) { SKIP(); } |
| 28 | SECTION( "also not skipped" ) { SUCCEED(); } |
| 29 | } |
| 30 | |
| 31 | TEST_CASE( "nested sections can be skipped dynamically at runtime", |
| 32 | "[skipping]" ) { |
| 33 | SECTION( "A" ) { std::cout << "a"; } |
| 34 | SECTION( "B" ) { |
| 35 | SECTION( "B1" ) { std::cout << "b1"; } |
| 36 | SECTION( "B2" ) { SKIP(); } |
| 37 | } |
| 38 | std::cout << "!\n"; |
| 39 | } |
| 40 | |
| 41 | TEST_CASE( "dynamic skipping works with generators", "[skipping]" ) { |
| 42 | const int answer = GENERATE( 41, 42, 43 ); |
| 43 | if ( answer != 42 ) { SKIP( "skipping because answer = " << answer ); } |
| 44 | SUCCEED(); |
| 45 | } |
| 46 | |
| 47 | TEST_CASE( "failed assertions before SKIP cause test case to fail", |
| 48 | "[skipping][!shouldfail]" ) { |
| 49 | CHECK( 3 == 4 ); |
| 50 | SKIP(); |
| 51 | } |
| 52 | |
| 53 | TEST_CASE( "a succeeding test can still be skipped", |
| 54 | "[skipping][!shouldfail]" ) { |
| 55 | SUCCEED(); |
| 56 | SKIP(); |
| 57 | } |
| 58 | |
| 59 | TEST_CASE( "failing in some unskipped sections causes entire test case to fail", |
| 60 | "[skipping][!shouldfail]" ) { |
| 61 | SECTION( "skipped" ) { SKIP(); } |
| 62 | SECTION( "not skipped" ) { FAIL(); } |
| 63 | } |
| 64 | |
| 65 | TEST_CASE( "failing for some generator values causes entire test case to fail", |
| 66 | "[skipping][!shouldfail]" ) { |
| 67 | int i = GENERATE( 1, 2, 3, 4 ); |
| 68 | if ( i % 2 == 0 ) { |
| 69 | SKIP(); |
| 70 | } else { |
| 71 | FAIL(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | namespace { |
| 76 | class test_skip_generator : public Catch::Generators::IGenerator<int> { |
| 77 | public: |
| 78 | explicit test_skip_generator() { SKIP( "This generator is empty" ); } |
| 79 | |
| 80 | auto get() const -> int const& override { |
| 81 | static constexpr int value = 1; |
| 82 | return value; |
| 83 | } |
| 84 | |
| 85 | auto next() -> bool override { return false; } |
| 86 | }; |
| 87 | |
| 88 | static auto make_test_skip_generator() |
| 89 | -> Catch::Generators::GeneratorWrapper<int> { |
| 90 | return { new test_skip_generator() }; |
| 91 | } |
| 92 | |
| 93 | } // namespace |
| 94 | |
| 95 | TEST_CASE( "Empty generators can SKIP in constructor", "[skipping]" ) { |
| 96 | // The generator signals emptiness with `SKIP` |
| 97 | auto sample = GENERATE( make_test_skip_generator() ); |
| 98 | // This assertion would fail, but shouldn't trigger |
| 99 | REQUIRE( sample == 0 ); |
| 100 | } |