我正在学习 TDD,使用 GoogleTest 框架。我已经成功构建了 Gtest,并且能够构建和运行示例。但是,当我尝试编写一个简单的示例时,我遇到了编译错误。
这是我使用的源代码和构建命令:
// ################################################
//proj1.h
#ifndef __SCRATCH_PROJ1_H
#define __SCRATCH_PROJ1_H
int addOne(int i);
#endif /*__SCRATCH_PROJ1_H */
// ################################################
//proj1.cpp
#include "proj1.h"
int addOne(int i){
return i+1;
}
// ################################################
//proj1_unittest.cpp
#include "proj1.h"
#include "gtest/gtest.h"
// Test Function
TEST(addOneTest, Positive) {
EXPECT_EQ(1,addOneTest(0)); // <- Line # 24
EXPECT_EQ(2,addOneTest(1)); // <- Line # 25
EXPECT_EQ(40320, addOneTest(40319)); // <- Line # 26
}
TEST(addOneTest, Negative) {
EXPECT_FALSE(addOneTest(-1)); // <- Line # 30
}
GTEST_API_ int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
控制台输出:
g++ -isystem ${GTEST_DIR}/include -pthread -c /home/user1/scratch/proj1_unittest.cpp /home/user1/scratch/proj1_unittest.cpp: In member function ‘virtual void addOneTest_Positive_Test::TestBody()’: /home/user1/scratch/proj1_unittest.cpp:24:5: error: ‘addOneTest’ was not declared in this scope /home/user1/scratch/proj1_unittest.cpp:25:5: error: ‘addOneTest’ was not declared in this scope /home/user1/scratch/proj1_unittest.cpp:26:5: error: ‘addOneTest’ was not declared in this scope /home/user1/scratch/proj1_unittest.cpp: In member function ‘virtual void addOneTest_Negative_Test::TestBody()’: /home/user1/scratch/proj1_unittest.cpp:30:5: error: ‘addOneTest’ was not declared in this scope
从错误消息中的行号来看,似乎没有定义 EXPECT_* 宏 - 但是,我已将 gtest/gtest.h 包含在编译单元中。
是什么导致了这些错误 - 我该如何解决?
最佳答案
正如它所说,addOneTest
没有在任何地方宣布。我猜你打算调用 addOne
反而。
关于c++ - Googletest 编译错误 : ‘xyzTest’ was not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18263464/