简体   繁体   English

跳过所有Googletest单元测试类的编译

[英]Skip compilation of all Googletest unit test classes

My project features many unit tests based on Googletest, ie classes inheriting from testing::Test . 我的项目包含许多基于Googletest的单元测试,即继承自tests testing::Test Each source file folder in the project has a subfolder test/ where these classes are located. 项目中的每个源文件文件夹都有一个子文件夹test/这些类所在的位置。

I would like users to be able to remove the dependency on Googletest. 我希望用户能够删除对Googletest的依赖。 What is the easiest way to do this? 最简单的方法是什么? Does Googletest provide some support? Googletest会提供一些支持吗?

One possible solution is to introduce a preprocessor variable NOUNITTESTS . 一种可能的解决方案是引入预处理器变量NOUNITTESTS When this is defined, all source depending on Googletest is skipped during compilation. 定义此选项后,编译期间将跳过依赖于Googletest的所有源。 But can I avoid going through all the test classes and surrounding them with #ifndef NOUNITTESTS ? 但是,我可以避免通过所有测试类并使用#ifndef NOUNITTESTS包围它们吗?

I don't understand why users depend on your project unit tests. 我不明白为什么用户依赖于您的项目单元测试。

Normally when you compile your binary or library you have targets to make that binary and completely different target to compile the unit tests. 通常,当您编译二进制文件或库时,您有目标来创建二进制文件和完全不同的目标来编译单元测试。 This way your users will never depend on your unit tests. 这样,您的用户将永远不会依赖于您的单元测试。

If you work with makefiles - just make a separate target for unit tests. 如果你使用makefile - 只需为单元测试制作一个单独的目标。

If you use Visual Studio - just make a separate project within solution for unit tests. 如果您使用Visual Studio - 只需在解决方案中为单元测试创​​建单独的项目。

Unit test are not part of the released product anyway. 无论如何,单元测试不是已发布产品的一部分。 For example, your project might have targets like clean , compile , install , test . 例如,您的项目可能具有cleancompileinstalltest Unless users run test target there will no be dependency on GTest . 除非用户运行test目标,否则不会依赖GTest

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

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