catchorg / Catch2

A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)
https://discord.gg/4CWS9zD
Boost Software License 1.0
18.65k stars 3.05k forks source link

Reusable BDD sections #2665

Open JeromeDesfieux opened 1 year ago

JeromeDesfieux commented 1 year ago

One of pros the BDD autotesting is to facilitate reusability. For example, you might have many tests needing the GIVEN("My algo is in step A") and you don't necessary want to push all those tests in the same scenario. The idea would be to write the corresponding code for "My algo is in step A" only once and being able to reuse it anywhere.

The usage might looks like something like that:

REGISTER_GIVEN( "A vector with some items" ) {   
    ...source code ...
}

GIVEN( "A vector with some items" ) {
    // no code here -> the registered code should expand here

    WHEN( "the size is reduced" ) {
          v.resize( 0 );

          THEN( "the size changes but not capacity" ) {
              REQUIRE( v.size() == 0 );
              REQUIRE( v.capacity() >= 5 );
          }
      }
}
GTruf commented 1 year ago

It would be a very cool feature if they implemented it in Catch2!