Catch测试框架问题:不能使用Catch::Session()

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

本文关键字:Catch Session 不能 测试 框架 问题      更新时间:2023-10-16

我在编写一些测试的c++文件中得到这个错误:

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

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

是否有任何宏设置使用会话类?

捕获版本:1.5.3和1.5.6。

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

您试图从没有定义要执行的main的文件中调用Catch::Session的构造函数。根据定义自己的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

您只能在定义CATCH_CONFIG_RUNNER的同一文件中提供main。

#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;
}