Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 448
Pietro Avatar asked Jun 29 '16 16:06

Pietro


2 Answers

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.

like image 62
jaggedSpire Avatar answered Sep 25 '22 14:09

jaggedSpire


Reference to https://github.com/catchorg/Catch2/blob/master/docs/own-main.md

You can only provide main in the same file you defined CATCH_CONFIG_RUNNER.

#define CATCH_CONFIG_RUNNER
#include "catch.hpp"

int main( int argc, char* argv[] ) {
  // global setup...

  int result = Catch::Session().run( argc, argv );

  // global clean-up...

  return result;
}
like image 24
Jagger Yu Avatar answered Sep 24 '22 14:09

Jagger Yu