简体   繁体   English

如何避免使用GoogleTest混合测试代码和生产代码?

[英]How to avoid mixing test and production code using GoogleTest?

I am starting to use GoogleTest. 我开始使用GoogleTest。 It seems that it needs a main file for running the tests: 似乎需要一个主文件来运行测试:

Separate test cases across multiple files in google test 在Google Test中将多个文件中的测试用例分开

But currently in my demo application I already have a main file: 但是目前在我的演示应用程序中,我已经有一个主文件:

src/
-> MyType.h
-> main.cpp
-> Makefile

Which will eventually be my "production" application. 最终将是我的“生产”应用程序。 I don't want to clutter that with gtest includes, macros etc. 我不想让gtest包含,宏等杂乱无章。

Should I just create another main.cpp file in another folder eg: test/ that will contain all the specific gtest configuration so I would end up with: 我是否应该在另一个文件夹中创建另一个main.cpp文件,例如: test/ ,它将包含所有特定的gtest配置,所以我最终会得到:

src/
-> MyType.h
-> main.cpp
-> Makefile // Makefile for producing production code/binaries
Test/
-> MyTypeTest.h // Unittest for MyType
-> main.cpp // The "Test runner"
-> Makefile // Makefile for producing test executable

EDIT: 编辑:

Found this based on cmake: 根据cmake找到了这个:

http://www.kaizou.org/2014/11/gtest-cmake/ http://www.kaizou.org/2014/11/gtest-cmake/

which seems to be exactly what I am looking for. 这似乎正是我想要的。

The most sensible approach to this is to have a library for your production code and then two executables, one for production and another one for tests: 最明智的方法是为生产代码提供一个库,然后提供两个可执行文件,一个用于生产,另一个用于测试:

|-lib/
| |-Makefile
| |-mytype.h
| `-mytype.cpp
|-app/
| |-Makefile
| `-main.cpp
`-test/
  |-Makefile
  `-mytypetest.cpp

Notice that gtest distribution provides the gtest library and a gtest_main library with the standard main function for your test executable. 请注意,gtest发行版为您的测试可执行文件提供了gtest库和gtest_main库以及标准main函数。 So unless you need a custom main (rare case) you don't need to provide a main.cpp for your tests and can simply link against gtest_main , eg $(CC) mytypetest.cpp -o apptests -lapplib -lgtest_main -lgtest . 因此,除非您需要自定义的main(罕见情况),否则不需要为测试提供main.cpp ,而只需链接gtest_main ,例如$(CC) mytypetest.cpp -o apptests -lapplib -lgtest_main -lgtest

The library approach involves slightly more complex Makefile s, but it pays off in compilation time, since not having it implies you need to compile mytype.cpp once for production application and once for test executable. 库方法涉及稍微复杂一些的Makefile ,但它在编译时间上mytype.cpp ,因为没有这种方法意味着您需要为生产应用程序一次编译mytype.cpp ,对于测试可执行文件一次编译一次。

There are probably a lot of ways to do this, but generally speaking, yes, you should add a test-specific main function to your project. 可能有很多方法可以做到这一点,但是总的来说,是的,您应该在项目中添加特定于测试的主要功能。 This makes compilation a little bit more complex since you'll have to produce two separate binaries (one for your application and another for your tests) but this is a fairly typical setup. 由于必须生成两个单独的二进制文件(一个用于您的应用程序,另一个用于您的测试),这使编译有点复杂。但这是一个相当典型的设置。

I'd simply add a test.cpp file with a main and create a test target in my makefile so that I could either make - to build my production code - or make test - to build the tests. 我只需要添加一个带有main的test.cpp文件,并在我的makefile中创建一个test目标,这样我就可以make -构建我的生产代码-或make test -构建测试。 In actual projects I use cmake in very similar fashion (I sometimes bundle all common dependencies in a core.a library and then link both main and test against it). 在实际的项目中,我以非常相似的方式使用cmake(有时我将所有常见的依赖项捆绑在core.a库中,然后将main链接起来并对其进行测试)。

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

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