简体   繁体   English

c ++ Google Tests运行两次

[英]c++ Google Tests run twice

I'm starting to use Google Test to run unit tests on my code. 我开始使用Google Test对我的代码运行单元测试。 I use Eclipse Kepler over Ubuntu 12.04. 我使用Eclipse Kepler而不是Ubuntu 12.04。

I'm using the following classes on this first test: 我在第一次测试中使用了以下类:

AllTests.cpp 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 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;
}
}

When I run the tests I get this output 当我运行测试时,我得到了这个输出

[==========] 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.

Why each test is being processed twice? 为什么每个测试都要处理两次?

Why do you include a translation unit in a translation unit? 为什么你包括在翻译单元翻译单元?

#include "SerialManagerTest.cpp"

It has its place in some cases, but usually is a bad practice. 它在某些情况下有它的位置,但通常是一种不好的做法。

What very likely happens (without seeing your command line), is that your SerialManagerTest code is linked twice because of the include in the final executable. 很可能发生的事情(没有看到你的命令行),是因为最终可执行文件中的include,你的SerialManagerTest代码被链接了两次。 That is, it is duplicated in AllTests.o and SerialManagerTest.o , and both objects are linked into the final test executable. 也就是说,它在AllTests.oSerialManagerTest.o重复,并且两个对象都链接到最终的测试可执行文件中。

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

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