Catch 测试框架问题:无法使用 Catch::Session()

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

我在编写一些测试的 C++ 文件中遇到此错误:

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

查看 catch.hpp 单个头文件,我注意到应该实现 Session() 成员函数的代码是灰色的,可能是因为某处的#ifdef,我找不到。

是否有任何宏可以设置为使用会话class?

捕获版本:1.5.3 和 1.5.6。

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

您正试图从一个文件中调用 Catch::Session 的构造函数,而您没有定义自己的 main 来执行。根据 documentation on defining your own main,应该只有一个实例 Catch::Session:

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

Catch 可能会阻止在翻译单元中构造 Catch::Session,因为它不能用于自定义 main 定义(因为这是它应该使用的地方),以完全防止你编译时犯的错误。

参考https://github.com/catchorg/Catch2/blob/master/docs/own-main.md

您只能在您定义的同一个文件中提供 main 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;
}