Give example for having tests in production code

1 min read Original article ↗

Description

I recently saw https://blog.jetbrains.com/rscpp/better-ways-testing-with-doctest/ where it was stated

What truly sets it apart is the ability to use it alongside your production code.

I would really like to try that. My expectation would be the following:

  • I have a large project containing of dozens of classes, each define in headers (.hpp) and implemented in source files (.cpp).
  • I would add test sections to each of the implementations (maybe at the bottom).
  • For "production" mode, I would compile the code as is - maybe defining or undefining some preprocessor symbol to switch off compilation of any test code.
  • For testing, I would like to reuse as much CMake code I already have and "just" recompile such that I have a single binary executing all the test distributed along the different implementation files.

Maybe my expectation is wrong, but in any case I would like to understand how the above quote is realizable in actual code.