Search code examples
c++unit-testingcatch-unit-test

Determine name of current SECTION


We are using Catch as testing framework and are quite satisfied with its usability.

I'm planning to implement a series of simple unit tests for a new chart widget. The chart widget can be only tested properly by simple image comparisons.

For this I need to compare the current image of the widget with a previously archived reference image. Now, I need to invent a filename for every test section, which is quite cumbersome and should be done automatically.

It is possible to derive the current SECTION in a TEST_CASE as a string?

The following example illustrates, what I wanted to achieve:

TEST_CASE("A") {
   SECTION("B") {
      SECTION("C") {
         std::string uniqueName=currentSection();
      }
   }
}

Now, uniqueName should at best contain something like A.B.C, but something unique would also suffices, if it stays the same after extending the test.


Solution

  • It seems like you're looking for a reliable 'path-like' naming scheme so that you can save previously-captures images for comparison, which is a great idea. But you also then talk about extending the test - if you add more sections, might the previously-captured image need renaming?

    Under the covers, Catch uses a macro to generate a unique TEST_CASE or SECTION name based on the filename and line number (docs) - this is not stable (nor meant to be) - it's just a way to have a unique reference. IIRC the test and section names are purely for human convenience. And there's unfortunately no way to access the name of the section: source