简体   繁体   中英

How to exclude Unit Test at compile time

I am currently refactoring parts of a qt/c++ application for my company and they have absolutely no unit tests.

I would like to add some, but I actually never did the setup for testing so I am not 100% sure of the best way to proceed. Here is what I was thinking (feel free to comment on this, but it is not my main question)

  • I'll use the QTest library since we use QT and I don't want to add another lib to the project
  • In each subfolder of the src directory, I'll add a test folder with test classes that correspond to the classes found in the directory (the goal is to keep test classes close to what they test)

My question is: How do I (or should I) prevent the test classes to be compiled / included in release? I mean, if I manage to get a large test coverage, this will be plenty of new test code to compile, which is useless for the application itself. Can we (should we) exclude this code in release versions? What is the common practice (if there is one)?

If you are using CMake, in your CMakeLists, you can define what files are used in your current build.

In the same CMake, you can define a different target that includes your tests files. The location of the files don't matter, they can be in the same place as your code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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