Search code examples
c++catch2

Create custom reporter for Catch2 in single header version


There is already a section in Catch2 documentation about how to create custom reporters. The problem is that this seems to work only for the non-single header version of Catch2.

Using single header version of Catch2, the two base classes for reporters ( Catch::StreamingReporterBase and Catch::CumulativeReporterBase) are not accessible.

They are not accessible because they are in a part of the header that is not included by the precompiler (by the way, I don't understand how default reporters work as they too are in not-included segments of the header).

Does someone know how to create custom reporters with single-header version of Catch2?

EDIT : I am using version 2 of Catch. The reporter base definitions are in a block disabled by a #ifdef OBJC block.


Solution

  • It depends on the version of the library you are using. Versions 2.x were based on the single header catch.hpp and had numerous additional macros to configure what is included from that header. In particular, you would need

    #define CATCH_CONFIG_EXTERNAL_INTERFACES
    #include "catch.hpp"
    

    to access the reporter definitions.

    With versions 3.x, this has changed and there are two options:

    • Adopt the new library architecture, meaning you link your targets against Catch2 and include the appropriate reporter header, e.g.
      #include <catch2/reporters/catch_reporter_streaming_base.hpp>
      
    • Use the catch_amalgamated.hpp/cpp files to compile the library as a single translation unit. Additional configuration should no longer be needed to make the reporter classes available.

    See also this document for more information on migrating to version 3.