Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio C++: Unit test exe project with google test?

Using Visual Studio 2010 C++. I'm experimenting with unit testing and decided to try Google Test (gtest). I have an existing project which compiles to an MFC executable (I'm also interested in how to test a project that compiles to a DLL). My understanding of the convention for unit testing is that you should create a new separate project for your tests. So I created a new project in the same solution for my unit tests. But how do I link the projects? Can I test arbitrary functions/methods of my exe project from my test project?

What is the conventional way to do this?

like image 209
User Avatar asked Jun 02 '11 15:06

User


2 Answers

I think the best way to organize unitary test is:

  • Don't change your main project. The structure should be independent of your test actions. In my opinion, changing your project to a big static lib AND an executable is really not elegant. Instead, add a post build action to aggregate all obj files into a static lib file that will be used ONLY by your test project.

  • Create a simple test project, linking to your test framework AND the static library you have previously generated.

  • Enjoy.

The main advantages is you don't touch the project you want to test and you don't include all source code to your test project.

To see how you can do that for visual studio, you can see this post: Linking to multiple .obj for unit testing a console application

like image 55
toussa Avatar answered Sep 18 '22 11:09

toussa


Either put the functionality you want to test into a static library which is linked into both your test project and your MFC project, or put your files in both projects. The first is more complicated, but the second will cause you to compile everything twice....

like image 42
Billy ONeal Avatar answered Sep 16 '22 11:09

Billy ONeal