Testing an executable project: including relevant header/source files in the test suite, redefinition of main() & co

Added by Lukasz Walewski about 1 year ago

Hello,

I was using CUTE for unit testing my library projects for some time now. The "CUTE Library Test Project" fits that purpose just perfectly. Upon setting up, it asks for the library project to be tested and takes over the settings exported by that project ("Export Settings" page in Project > Properties > C/C++ General). In particular the include (-I) and link (-L) directories necessary to compile and link the tests get added to the test project, as expected.

Now I need to test my executable projects as well, which brings me to my actual question. In order to compile and link my test suite, which tests classes in some "C++ Executable" project, the compiler needs to know the location of header and implementation files of the tested classes. How this is done "the CUTE way"?

My executable project does not export any settings as it is meant as a standalone application. Moreover, including the whole project's source directory invokes the problem of having main() declared twice - once in the test suite and once in the tested project. One way to go would be to create a "Linked Folder" in the test project, which would point to the source directory in my executable project and then use "Resource Filters" to include only relevant header/source files. This approach is, however, pretty complicated and thus error prone.

I would be grateful for recommendations, on how to achieve my goal the better way.

Best regards,
Lukasz


Replies (1)

RE: Testing an executable project: including relevant header/source files in the test suite, redefinition of main() & co - Added by Thomas Corbat about 1 year ago

Hi Lukasz

Unfortunately, we don't have a great solution for this case.
You could separate your main() function from any testable functionality. I.e. create an executable project containing only a main.cpp with a main() function that calls your code:

int main() {
  program(std::cin, std::cout);
}

Now you could move program() and everything else to a static library project. You can write tests for program() and other functions.

I know this is not the most convenient approach. But we don't have an appropriate alternative for testing executable projects (yet).

Best Regards
Thomas

(1-1/1)