Search code examples
c++sessiontestingframeworkscatch-unit-test

Catch test framework issue: cannot use Catch::Session()


I get this error in a C++ file where I am writing some tests:

error: no member named 'Session' in namespace 'Catch'
        testResult = Catch::Session().run(test_argc, test_argv);
                     ~~~~~~~^

Looking at the catch.hpp single header file, I noticed that the code that should implement the Session() member function is greyed out, probably because of an #ifdef somewhere, which I cannot find.

Is there any macro to set to use the Session class?

Catch versions: 1.5.3 and 1.5.6.

Reference: https://github.com/philsquared/Catch/blob/master/docs/own-main.md


Solution

  • You're attempting to call the constructor of Catch::Session from a file where you're not defining your own main to execute. According to the documentation on defining your own main, there is supposed to be only one instance of Catch::Session:

    Catch::Session session; // There must be exactly once instance
    

    It's likely Catch is preventing construction of Catch::Session in translation units where it can't be used in a custom main definition (since that's where it's supposed to be used), to prevent exactly the mistake you've made from compiling.