简体   繁体   English

是否可以将数据传递到c ++ Google测试框架中的TestEventListener中?

[英]Is it possible to pass data into a TestEventListener in the c++ Google test framework?

I am using my own printer class in the google test framework to get some custom printing, as shown below: 我在google测试框架中使用自己的打印机类来进行一些自定义打印,如下所示:

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());
    }
};

In my main() I am installing this printer as follows: 在我的main()中,我按照以下方式安装此打印机:

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

Is it possible to pass user data into the virtual method OnTestEnd of the custom printer? 是否可以将用户数据传递到自定义打印机的虚拟方法OnTestEnd中? I want to report some extra data that was gathered. 我想报告一些收集的额外数据。

I don't think you can do that without changing Google Test sources. 我认为您不能在不更改Google Test来源的情况下做到这一点。

You would need to subclass TestInfo in order to append your custom data to the object, but you have no control over the instantiation of the actual parameter passed by to TestEventListener::OnTestEnd . 您需要将TestInfo子类TestInfo ,以便将自定义数据附加到对象,但是您无法控制传递给TestEventListener::OnTestEnd的实际参数的实例化。 The only way to control this instantiation is to modify Google Test source code so the data you want to gather is available at the point of calling OnTestEnd . 控制此实例化的唯一方法是修改Google Test源代码,以便在调用OnTestEnd可以使用要收集的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM