Search code examples
c++catch2

Is there a Catch2 variable that provides the name of the current test?


I'm using Catch2 for unit tests and I would like to know the name of a C++ or environment variable that reveals the name of the current test that is executing.

Is there a way to do this, or is it bad form to have the test debugging code print the name of the test in which it is running? Ideally I'd like to print it using the CAPTURE Macro.

The reason that I want to do this is that I have many Catch2 tests and some of them generate voluminous output. Some generate the exact same output. Some generate output that should be the same but sometimes isn't. The output is not generated in the body of the unit test, but instead in a function called. So I would like to be able to label the output according to the macro that generated it.

I suppose that one way to do this would be to use a scaffold that stores the name into a global variable, or an environment variable.


Solution

  • I think your scaffolding idea is the way to go. I have not found any macros that would return the name without having to define an extra variable.

    When it comes to the class that is being instantiated for a test case:

    struct AutoReg : Detail::NonCopyable {
        AutoReg( Detail::unique_ptr<ITestInvoker> invoker, SourceLineInfo const& lineInfo, StringRef classOrMethod, NameAndTags const& nameAndTags ) noexcept;
    };
    

    It doesn't have any members that would hold test info and even if it did you'd have to keep track of the unique name it's given for each test.

    Looks like AutoReg simply modifies members of a class called RegistryHub, which holds the test info in vectors.

    In my opinion at this point you're better off keeping track of test names yourself.