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

后端 未结 6 1755
闹比i
闹比i 2020-12-22 20:59

I use Google C++ Testing Framework for unit testing of my code. I use Eclipse CDT with C++ Unit testing module for output analysis.

Previously I used CppUnit it ha

6条回答
  •  春和景丽
    2020-12-22 21:42

    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:

    Example 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()
    

提交回复
热议问题