Path of least resistance when unit testing C++ code in an exe, in Visual Studio 2012

c++, unit-testing, visual-studio-2012

Solution

This will depend on how you have your solution structured. The way I like to structure my solutions is to have three projects.

- A .lib project that has my source code in it.

- An executable project, linked with the .lib. This calls into the .lib in the `main()` call

- A test project (exe), linked with the .lib.

With this structure you can use the `Add New Reference...` button in the `Common Properties` section and the references will be sorted for you (except the header include path found in `C++\General\Additional include directories`).

If you do not want to restructure your projects you can tell the linker about each obj file (`Linker\Input\Additional dependencies`). This may be a significant number of .obj files if you have a lot of classes that you want to test. Unfortunately, you may have issues if you use pre-compiled headers.

I would suggest restructuring the projects if you can.

Problem

I'm in need of some sage advice here. Long story short, I'm rebuilding a - for me - relatively complex app comprised of about 7000 lines of code. I ran into a number of issues when I created the first iteration of my application and it seems to me that test driven development might just be the ticket. I was pleased to see that Visual Studio 2012 now natively supports TDD in C++, so I went ahead and read as much as I could. Unfortunately, Vs2012 is fairly new and I feel the documentation is somewhat lacking. But this is a little beside the point. I'm relying mainly on the following guide on the MSDN site: http://msdn.microsoft.com/en-us/library/hh419385.aspx#objectRef It fairly clearly states that if the code under testing is to be built as an .exe, then the way forward is creating a separate test project and linking the output object file. I'm guessing they mean the object files? Or maybe not? I'm honestly a little confused as to how many .obj's I need to link. At first I thought I needed to link every single obj file which is fairly tedious. If anyone has experience doing this and could perhaps also recommend which macros or similar short cuts to use in order to make this process as painless as possible, I'd be much obliged!

Original source

Related problems