c++catch-unit-test

Getting sections that will be run in Catch


The Catch2 unit test framework allows you to have test sections. From the docs:

TEST_CASE( "vectors can be sized and resized", "[vector]" ) {

    std::vector<int> v( 5 );

    REQUIRE( v.size() == 5 );
    REQUIRE( v.capacity() >= 5 );

    SECTION( "resizing bigger changes size and capacity" ) {
        v.resize( 10 );

        REQUIRE( v.size() == 10 );
        REQUIRE( v.capacity() >= 10 );
    }
    SECTION( "resizing smaller changes size but not capacity" ) {
        v.resize( 0 );

        REQUIRE( v.size() == 0 );
        REQUIRE( v.capacity() >= 5 );
    }

    // ...
}

Is there a way to identify up front, at the time of testCaseStarting(), what is the list of SECTIONs that the particular run? As an example, given:

TEST_CASE("a", "[tag]") {
    SECTION("b") {
    }

    SECTION("c") {
        SECTION("d") { }
        SECTION("e") { }
    }
}

I want some way to get {b} for the first run, {c, d} for the second, and {c, e} for the third. Is there any way to do this?


Solution

  • I don't think so. SECTION expands into INTERNAL_CATCH_SECTION which is just an if statement creating SectionInfo class instance :

       #define INTERNAL_CATCH_SECTION( ... ) \
        if( Catch::Section const& INTERNAL_CATCH_UNIQUE_NAME( catch_internal_Section ) = Catch::SectionInfo( CATCH_INTERNAL_LINEINFO, __VA_ARGS__ ) )