繁体   English   中英

c ++ Google Tests运行两次

[英]c++ Google Tests run twice

我开始使用Google Test对我的代码运行单元测试。 我使用Eclipse Kepler而不是Ubuntu 12.04。

我在第一次测试中使用了以下类:

AllTests.cpp

#include "gtest/gtest.h"
#include "SerialManagerTest.cpp"

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

SerialManagerTest.cpp

#include "gtest/gtest.h"
#include "SerialManager.h"
#include "SerialInterface.h"
#include "FakeSerialHandler.h"

namespace {

TEST(TestingSerialManager, FirstTest) {
  SerialInterface *fakeSerialHandler=new FakeSerialHandler();
  SerialManager* serialManager=new SerialManager(fakeSerialHandler);
  ASSERT_TRUE(serialManager->OpenPort());

  delete serialManager;
}

TEST(TestingSerialManager, SecondTest) {
SerialInterface *fakeSerialHandler=new FakeSerialHandler();
SerialManager* serialManager=new SerialManager(fakeSerialHandler);
ASSERT_FALSE(!serialManager->OpenPort());

delete serialManager;
}
}

当我运行测试时,我得到了这个输出

[==========] Running 4 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 4 tests from TestingSerialManager
[ RUN      ] TestingSerialManager.FirstTest
[       OK ] TestingSerialManager.FirstTest (0 ms)
[ RUN      ] TestingSerialManager.SecondTest
[       OK ] TestingSerialManager.SecondTest (0 ms)
[ RUN      ] TestingSerialManager.FirstTest
[       OK ] TestingSerialManager.FirstTest (0 ms)
[ RUN      ] TestingSerialManager.SecondTest
[       OK ] TestingSerialManager.SecondTest (0 ms)
[----------] 4 tests from TestingSerialManager (2 ms total)

[----------] Global test environment tear-down
[==========] 4 tests from 1 test case ran. (3 ms total)
[  PASSED  ] 4 tests.

为什么每个测试都要处理两次?

为什么你包括在翻译单元翻译单元?

#include "SerialManagerTest.cpp"

它在某些情况下有它的位置,但通常是一种不好的做法。

很可能发生的事情(没有看到你的命令行),是因为最终可执行文件中的include,你的SerialManagerTest代码被链接了两次。 也就是说,它在AllTests.oSerialManagerTest.o重复,并且两个对象都链接到最终的测试可执行文件中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM