7

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?

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
User
  • 62,498
  • 72
  • 186
  • 247
  • Related question basically asking the same thing which I didn't find when I searched for this subject: http://stackoverflow.com/questions/2680892/how-do-you-run-your-unit-tests-compiler-flags-static-libraries – User Jun 02 '11 at 22:38

3 Answers3

10

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

Community
  • 1
  • 1
toussa
  • 1,058
  • 12
  • 21
6

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....

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • The first option is what we've done and it works well. I think the second option would get irritating quickly for anything but small, or rarely changing, projects :) – Jan Jun 02 '11 at 16:14
  • What do you mean put files in both projects? Are you talking about having copies of the files in both projects or do you mean adding the same file on disk to both projects? – User Jun 02 '11 at 16:15
  • @User I would expect symlinking or hardlinking – alternative Jun 02 '11 at 16:37
  • @User: You just add the same files to both projects. No need to have multiple files. You do that using the "Add existing item" thingy -- it will add the files to the project but will not copy the files themselves. – Billy ONeal Jun 02 '11 at 16:52
  • Thinking about this more it looks like I should extract my classes into a static library. Is there any performance impact for doing this? – User Jun 02 '11 at 22:34
  • 1
    @User: No. A static library is just a collection of .objs. – Billy ONeal Jun 02 '11 at 22:52
3

I have prepared a github repo including Visual Studio 2015 solution in parralel of Billy's answer. You can use it directly without any additional requirement or dependency.

https://github.com/fuatcoskun/GoogleTestVS2015

I hope it helps...

Fuat Coşkun
  • 1,045
  • 8
  • 18
  • When I build this, I do not see an output. Any thoughts? Do I need to add a getchar() somewhere to pause the output? – JC203 Mar 09 '17 at 16:20