简体   繁体   中英

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?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM