How to send custom message in Google C++ Testing Framework?

醉酒当歌 提交于 2019-12-02 14:11:37

The gtest macros return a stream for outputting diagnostic messages when a test fails.

EXPECT_TRUE(false) << "diagnostic message"; 

There is no way of doing it cleanly in the current version of gtest. I looked at the code, and the only text output (wrapped in gtest "Messages") is shown if you fail a test.

However, at some point, gtest starts printf'ing to the screen, and you can leverage the level above that to get colors that are platform independent.

Here's a hacked macro to do what you want. This uses the gtest internal text coloring. Of course the internal:: namespace should be sounding off warning bells, but hey, it works.

Usage:

TEST(pa_acq,Foo) {   // C style   PRINTF("Hello world \n");    // or C++ style    TEST_COUT << "Hello world" << std::endl; } 

Output:

Code:

namespace testing {  namespace internal  {   enum GTestColor {       COLOR_DEFAULT,       COLOR_RED,       COLOR_GREEN,       COLOR_YELLOW   };    extern void ColoredPrintf(GTestColor color, const char* fmt, ...);  } } #define PRINTF(...)  do { testing::internal::ColoredPrintf(testing::internal::COLOR_GREEN, "[          ] "); testing::internal::ColoredPrintf(testing::internal::COLOR_YELLOW, __VA_ARGS__); } while(0)  // C++ stream interface class TestCout : public std::stringstream { public:     ~TestCout()     {         PRINTF("%s",str().c_str());     } };  #define TEST_COUT  TestCout() 

There is a quite simple and hacky way for doing it (without need of diving into internal classes or creating new custom classes).

Just define a macro:

#define GTEST_COUT std::cerr << "[          ] [ INFO ]" 

and use GTEST_COUT (just like cout ) in your tests :

GTEST_COUT << "Hello World" << std::endl; 

And you'll see such result:

Credit goes to @Martin Nowak for his finding.

Refer to Mark Lakata's answer, here is my way:

Step1: create a header file, for example: gtest_cout.h

Code:

#ifndef _GTEST_COUT_H_ #define _GTEST_COUT_H_  #include "gtest/gtest.h"  namespace testing { namespace internal { enum GTestColor {     COLOR_DEFAULT, COLOR_RED, COLOR_GREEN, COLOR_YELLOW }; extern void ColoredPrintf(GTestColor color, const char* fmt, ...); } }  #define GOUT(STREAM) \     do \     { \         std::stringstream ss; \         ss << STREAM << std::endl; \         testing::internal::ColoredPrintf(testing::internal::COLOR_GREEN, "[          ] "); \         testing::internal::ColoredPrintf(testing::internal::COLOR_YELLOW, ss.str().c_str()); \     } while (false); \  #endif /* _GTEST_COUT_H_ */ 

Step2: use GOUT in your gtest

Usage:

#include "gtest_cout.h"  TEST(xxx, yyy) {     GOUT("Hello world!"); } 
허영주

You should define the below:

static class LOGOUT { public:     LOGOUT() {}     std::ostream&  info() {         std::cout << "[info      ] ";         return std::cout;     }  } logout; 

using this:

logout.info() << "test: " << "log" << std::endl; 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!