Separating main() for Testing
Explore how to separate the main() function in a C++ project to enable efficient testing with CMake. Learn to create executable targets that share the same code base but allow independent testing and production runs, improving build validation and automation.
We'll cover the following...
Testing the build
As we have established so far, a linker enforces the ODR and makes sure that all external symbols provide their definitions in the process of linking. One interesting problem that we might encounter is the correct testing of the build.
Ideally, we should test exactly the same source code that is being run in production. An exhaustive testing pipeline should build the source code, run its tests on produced binary, and only then package and distribute the executable (without the tests themselves).
But how do we actually make this happen? Executables have a very specific flow of execution, which often requires reading command-line arguments. C++'s compiled nature doesn't really support pluggable units that can be temporarily injected into the ...