是否可以将数据传递给C++谷歌测试框架中的TEST监听器?

是否可以将数据传递给C++谷歌测试框架中的TEST监听器?,c++,googletest,C++,Googletest,我在google测试框架中使用自己的printer类来获得一些自定义打印,如下所示: class bsgtDefaultPrinter : public ::testing::EmptyTestEventListener { // Called before a test starts. virtual void OnTestStart(const ::testing::TestInfo& test_info) { printf("*** Starting t

我在google测试框架中使用自己的printer类来获得一些自定义打印,如下所示:

class bsgtDefaultPrinter : public ::testing::EmptyTestEventListener {
    // Called before a test starts.
    virtual void OnTestStart(const ::testing::TestInfo& test_info) {
      printf("*** Starting test %s.%s\n", test_info.test_case_name(), test_info.name());
    }

    // Called after a failed assertion or a SUCCEED() invocation.
    virtual void OnTestPartResult(const ::testing::TestPartResult& test_part_result) {
        std::string msg = test_part_result.summary();
        char c = '|';
        msg = StripNewlines(msg,&c); // replace newlowith bar so result is all on same line
        printf("   %s%s\n",test_part_result.failed() ? "*** " : "",msg.c_str());
    }

    // Called after a test ends.
    virtual void OnTestEnd(const ::testing::TestInfo& test_info) {
        if (test_info.result()->Failed())
            printf("*** FAIL: ");
        else
            printf("*** PASS: ");
        printf("test %s.%s\n\n", test_info.test_case_name(), test_info.name());
    }
};
我主要按照以下步骤安装此打印机:

::testing::TestEventListeners& listeners = ::testing::UnitTest::GetInstance()->listeners();
delete listeners.Release(listeners.default_result_printer());
listeners.Append(new bsgtDefaultPrinter);
RUN_ALL_TESTS();

是否可以将用户数据传递到自定义打印机的虚拟方法OnTestEnd?我想报告收集到的一些额外数据。

我认为如果不更改Google测试源,您无法做到这一点


您需要将TestInfo子类化,以便将自定义数据附加到对象中,但您无法控制传递给TestEventListener::OnTestEnd的实际参数的实例化。控制此实例化的唯一方法是修改Google测试源代码,以便您要收集的数据在调用OnTestEnd时可用。

是的,我很担心。我甚至不能在printer类中缓存数据,因为这不是线程安全的