如何在 Google C + + 测试框架中发送自定义消息?

我使用 Google C + + 测试框架对代码进行单元测试。 我使用 Eclipse CDT 和 C + + 单元测试模块进行输出分析。

以前我用的是 CppUnit,它有宏家族 消息,可以这样称呼:

CPPUNIT_ASSERT_EQUAL_MESSAGE("message",EXPECTED_VALUE,ACTUAL_VALUE)

并允许发送自定义消息以测试输出。

有没有办法包括一些自定义文本在谷歌测试输出?

(最好的方式,可以包括消息的数据读取现有的程序自动单元测试使用谷歌测试。)

74838 次浏览

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:

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

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;

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!");
}

There is a quite simple and hacky way for doing it (without the 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 the result:

enter image description here

Credit goes to @Martin Nowak for his finding.

From the Advanced googletest Topics you can use a few macros for that purpose.

  • SUCCEED() SUCCEED() << "success/info message"; SUCCEED() only outputs your message and proceeds. It does not mark test as passed. Its result will be determined by the following asserts.
  • FAIL() FAIL() << "test failure message"; FAIL() marks your test as failed, outputs your message and then returns from the function. Therefore can only be used in functions returning void.
  • ADD_FAILURE() ADD_FAILURE() << "test failure message"; ADD_FAILURE() marks your test as failed and outputs your message. It does not return from the calling function and execution flow continues like with EXPECT_ series of macros.