Get error message from a Google Test after running it

Viewed 623

I'm implementing a custom runner for Google Testing Framework that runs a concrete TestInfo separately and checks its status:

TestInfo* info = ...;
testing::GTEST_FLAG(filter) = std::string(info->test_case_name()) + "." + info->name();
RUN_ALL_TESTS();
std::cout << (info->result()->Passed() ? "Success :)" : "Error :(") << std::endl;

Here testInfo->result() has type TestResult, which has methods that provide information on whether the test has passed or failed, was the failure critical, and also properties which can be set by user.

But google tests can also have a failure message, i. e. FAIL() << "Division by zero";. Is there any way to retrieve this failure message from the Google Test api?

1 Answers

Just downloaded GitHub repo of googletest and looked through code. Seems that your FAIL() message can be retrieved by next code (don't have any gtest project to test correctness of code):

for (auto const & pr:
      testing::internal::TestResultAccessor::test_part_results(*info->result()))
    if (pr.fatally_failed())
        std::cout << "FAIL(): " << pr.message() << std::endl;

Please, correct me if I'm wrong, I'll update my answer.

UPDATE. Just found less hacky way (without using ::internal) after looking at code, also not tested:

for (int i = 0; i < info->result()->total_part_count(); ++i) {
    auto const & pr = info->result()->GetTestPartResult(i);
    if (pr.fatally_failed())
        std::cout << "FAIL(): " << pr.message() << std::endl;
}
Related