【发布时间】:2016-03-16 16:00:20
【问题描述】:
我尝试在源代码中使用调试标志添加 (GOOGLE_TEST) 并在 TEST/Makefile.am 中定义它。但事情没有奏效。我正在使用 C++ 语言。 注意:我不想更改 SRC 目录代码中会影响生产代码及其 Makefile.am 的任何内容
SRC 目录中的测试类
class Common: public Thread {
public:
friend class test_common;
Common {
}
~Common () {
}
virtual void ThreadMain();
protected:
virtual void ProcessData(void);
};
void Common::ProcessData(void) {
#ifndef __GOOGLE_TEST__
while (1) { }
#endif
}
测试目录中的TESTCODE
class test_common : public ::testing::Test {
};
TEST_F(test_common, create_common) {
Common commonObj();
commonObj. ProcessData ();
}
输出
即使在 test/makefile.am 中定义了标志之后,GTest 仍卡在 While 循环部分
【问题讨论】:
-
你是在编译SRC目录中的代码时定义
__GOOGLE_TEST__,还是只在编译测试时定义__GOOGLE_TEST__? -
@Antonio Pérez:仅在编译测试时。
-
那就是原因:)
-
@Antonio Pérez:如果我在源代码 make 文件中定义,那么该功能将不适用于他们 :-)
-
那么不要在源代码中定义它而是作为编译选项,即
-D__GOOGLE_TEST__,仅在编译测试时。无论如何,用于测试的条件编译通常表明您应该将不想在测试中运行的代码分解到不同的类中,然后注入依赖项,以便您可以在测试中模拟它。
标签: c++ unit-testing c++11 googletest gmock