Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

googletest

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 has macros family CPPUNIT*_MESSAGE that could be called like this:

CPPUNIT_ASSERT_EQUAL_MESSAGE("message",EXPECTED_VALUE,ACTUAL_VALUE) 

And allows to send custom messages to test output.

Is there a way to include some custom text in google test output?

(Preferably the way that could include message to data that is read by existing programs for automated unit testing using google test.)

like image 790
Yuriy Petrovskiy Avatar asked May 10 '13 22:05

Yuriy Petrovskiy


People also ask

Does Google Test support C?

As all Google's C++ code, Google Test does not use exceptions, so exception safety flow won't be an issue. As long as your headers are C++-compatible (not using C++ keywords, export symbols with correct linkage), it should be fine.

How do you write a value parameterized test?

To write value-parameterized tests, first you should define a fixture class. It must be derived from both testing::Test and testing::WithParamInterface<T> (the latter is a pure interface), where T is the type of your parameter values.

What is Test_f in Gtest?

TEST_F(TestFixtureName, TestName) { ... statements ... } Defines an individual test named TestName that uses the test fixture class TestFixtureName . The test suite name is TestFixtureName .

What is InitGoogleTest?

The ::testing::InitGoogleTest method does what the name suggests—it initializes the framework and must be called before RUN_ALL_TESTS . RUN_ALL_TESTS must be called only once in the code because multiple calls to it conflict with some of the advanced features of the framework and, therefore, are not supported.


2 Answers

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

EXPECT_TRUE(false) << "diagnostic message"; 
like image 188
user2093113 Avatar answered Sep 25 '22 04:09

user2093113


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() 
like image 29
Mark Lakata Avatar answered Sep 23 '22 04:09

Mark Lakata