|
|
飘逸的野马 · 在 Excel 中将命令发送到程序时出错 ...· 3 月前 · |
|
|
朝气蓬勃的火腿肠 · 成都百年地标,回来了_澎湃号·湃客_澎湃新闻 ...· 10 月前 · |
|
|
喝醉的米饭 · 歌剧《茶花女》――缘自眷恋的永恒(周皓)_中 ...· 1 年前 · |
|
|
飞奔的可乐 · 南京重启购房落户 深圳发起以旧换新 ...· 2 年前 · |
|
|
深情的脆皮肠 · 哥布林的窑洞1未增删/哥布林洞窟1—3动画/ ...· 2 年前 · |
|
|
文武双全的书包 · [郭于华]代际关系中的公平逻辑及其变迁 ・ ...· 2 年前 · |
我使用 谷歌C++测试框架 对代码进行单元测试。我使用 带C++单元测试模块的Eclipse 进行输出分析。
以前我使用过 CppUnit ,它有宏家族 消息 ,可以这样调用:
CPPUNIT_ASSERT_EQUAL_MESSAGE("message",EXPECTED_VALUE,ACTUAL_VALUE)
并允许发送自定义消息来测试输出。
有办法在google测试输出中包含一些自定义文本吗?
(最好是将消息包含到现有程序读取的数据中,以便使用google测试进行自动单元测试。)
gtest宏返回一个流,用于在测试失败时输出诊断消息。
EXPECT_TRUE(false) << "diagnostic message";
在当前版本的gtest中,没有办法干净地这样做。我查看了代码,如果测试失败,将显示唯一的文本输出(包装在gtest“Messages”中)。
但是,在某个时候,gtest开始对屏幕进行
printf
'ing,您可以利用上面的级别获得与平台无关的颜色。
这里有一个被黑的宏来做你想做的事。这使用了最普通的内部文本着色。当然,
internal::
名称空间应该会敲响警钟,但是它可以工作。
用法:
TEST(pa_acq,Foo)
// C style
PRINTF("Hello world \n");
// or C++ style
TEST_COUT << "Hello world" << std::endl;
}
输出:
代码:
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()
您应该定义如下:
static class LOGOUT {
public:
LOGOUT() {}
std::ostream& info() {
std::cout << "[info ] ";
return std::cout;
} logout;
使用此方法:
logout.info() << "test: " << "log" << std::endl;
请参考Mark Lakata的回答,这是我的方法:
gtest_cout.h
Step1:创建一个头文件,例如:Step1
代码:
#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) \
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); \
|
|
喝醉的米饭 · 歌剧《茶花女》――缘自眷恋的永恒(周皓)_中国作家网 1 年前 |