But I already do that! And its extremely easy to do with my method! Probably dramatically more so than any other way. I simply add a
#include “filename.test.cpp”
to my single everything.cpp file just after#include “filename.cpp”
and I am instantly productive.
Even better I can change the entire configuration/implementations by just changing a few > template parameters!
Ishh... You actually have to modify code (by adding an include statement or changing a few template parameters) to add the unit-tests... that's very ugly, sorry. There are two common ways to setup unit-tests:
1) You put the "main()" function for the unit-test at the end of the cpp file containing the functions that are tested by the unit-test program, but you wrap that main() function with a compilation flag. As so:
// in a cpp-file:
#include "my_functions.h"
void foo() {
//...
};
//... other function implementations.
#ifdef MY_LIB_COMPILING_UNIT_TESTS
int main() {
//... all the unit test code.
//... usually returns 0 for success and an error code otherwise.
};
#endif
2) You create a separate cpp-file for each unit-test and link it to your library.
The advantage of either of these methods is that you have no intrusion at all on your library's code itself. In order words, without changing anything in the code, you can seeminglessly compile all your code into static or shared libraries (and/or a set of executable applications) without leaving a trace of the unit-tests, and you can easily compile …