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.)
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.
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.
TEST_F(TestFixtureName, TestName) { ... statements ... } Defines an individual test named TestName that uses the test fixture class TestFixtureName . The test suite name is TestFixtureName .
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With