如何在 Google C++ 测试框架中发送自定义消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16491675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send custom message in Google C++ Testing Framework?
提问by Yuriy Petrovskiy
I use Google C++ Testing Frameworkfor unit testing of my code. I use Eclipse CDT with C++ Unit testing modulefor output analysis.
我使用Google C++ 测试框架对我的代码进行单元测试。我使用带有 C++ 单元测试模块的 Eclipse CDT进行输出分析。
Previously I used CppUnitit has macros family CPPUNIT*_MESSAGEthat could be called like this:
以前我使用CppUnit它有宏系列 CPPUNIT*_MESSAGE可以这样调用:
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.)
(最好是可以包含现有程序读取的数据消息的方式,以便使用谷歌测试进行自动化单元测试。)
回答by user2093113
The gtest macros return a stream for outputting diagnostic messages when a test fails.
gtest 宏返回一个流,用于在测试失败时输出诊断消息。
EXPECT_TRUE(false) << "diagnostic message";
回答by Mark Lakata
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 faila test.
在当前版本的 gtest 中没有办法干净利落地做到这一点。我查看了代码,如果测试失败,则会显示唯一的文本输出(包装在 gtest“消息”中)。
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.
但是,在某些时候,gtest 开始printf
出现在屏幕上,您可以利用高于该级别的级别来获得与平台无关的颜色。
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.
这是一个黑客宏来做你想做的事。这使用 gtest 内部文本着色。当然,internal::
命名空间应该敲响警钟,但是,嘿,它有效。
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()
回答by Just Shadow
There is a quite simpleand hacky way for doing it (without need of diving into internal classes or creating new custom classes).
有一种非常简单和 hacky 的方法来做到这一点(无需深入研究内部类或创建新的自定义类)。
Just define a macro:
只需定义一个宏:
#define GTEST_COUT std::cerr << "[ ] [ INFO ]"
and use GTEST_COUT
(just like cout
) in your tests :
并在您的测试中使用GTEST_COUT
(就像cout
):
GTEST_COUT << "Hello World" << std::endl;
And you'll see such result:
你会看到这样的结果:
Credit goes to @Martin Nowakfor his finding.
幸得@马丁·诺瓦克,他的发现。
回答by yosolin
Refer to Mark Lakata's answer, here is my way:
参考 Mark Lakata 的回答,这是我的方法:
Step1: create a header file, for example: gtest_cout.h
Step1:创建头文件,例如: 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
Step2:GOUT
在你的gtest中使用
Usage:
用法:
#include "gtest_cout.h"
TEST(xxx, yyy)
{
GOUT("Hello world!");
}
回答by ???
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;