Google 单元测试混乱

Google Unit Test Confusion

我对单元测试应该在哪里感到有点困惑。所有文档使我相信我必须创建一个测试项目。真的吗?使用 JUnit,我们刚刚在我们的应用程序项目中创建了一个测试包(文件夹),并且每次构建都会进行测试 运行。

我是 C++ 的新手,正在尝试了解 Google Test 的工作原理。我在 SO 上发现了很多非常好的问题和答案,是的,我已经阅读了 Google 测试文档,但我仍然对这一件事感到困惑:

Can I create a subfolder in my project with all my unit tests, such that they will execute with each build?

如果是,那么你能帮我找到需要的吗information/example?

好的,我在这里的 Primer 中找到了:https://github.com/google/googletest/blob/master/googletest/docs/primer.md

一旦您能够编译 Google 测试库,您应该为您的测试程序or build target创建一个项目。这是有前途的!也许文档只是一直提到创建项目,并没有明确说明测试代码可以在您的应用程序项目中?

Nope! Just wishful thinking on my part, as πάντα-ῥεῖ pointed out in his answer.

"All the documentation leads me to believe that I must create a Test Project. REALLY?"

是的,对于 C++,你必须这样做,因为没有通用的 test 运行ner 可执行工件,因为它是 JUnit 提供的。

Can I create a subfolder in my project with all my unit tests, such that they will execute with each build?

我建议将所有测试用例 类(作为普通的 .cpp 来源)放入一个单独的项目中,并且 link 和 类 正在测试中一个单独的库项目。在 main() 函数中包含 gtest_all.cc,或在 gtest 库中包含 link,以及测试项目。

向 运行 测试用例添加 运行ning 来自该项目的 UnitTester 工件构建作为额外的构建步骤。


"Maybe the documentation just keeps mentioning creating a project and doesn't make it clear that the test code can be in your application project."

好吧,这在很大程度上取决于您的实际 IDE/build 系统什么是单个项目(因此我更喜欢将此术语用于单个工件),以及 解决方案(或 workspace)以获得它们的相关集合。