Search code examples
c++unit-testingvisual-studio-2012vs-unit-testing-framework

Unit testing c++ console application in Visual Studio 2012


I am trying to create a unit test project in Visual Studio. The code I want to test is in c++ and I want to write the tests in C++. Is this possible?

Also, all I find is about projects that export static or dynamic libraries. My project exports nothing, it's a simple console application, and I don't want to turn it into a library.

I have tried various tutorials on the net and all I get is either guideliness about testing static/dynamic libraries (which I know nothing about) or things that end up with me getting the unresolved external symbol.. error when compiling the test.

Is there a unit test, project template where I just declare where is the solution I want to test?


Solution

  • Yes, it is possible, you simply need to add the .obj or .lib file(s) of the project under test to the dependencies of the test project.

    • File -> New -> Project -> Visual C++ -> Native unit test project (create the test project)
    • In the new project open the project Properties
    • Properites -> Configuration Properties, Linker, Input, Additional Dependencies : Add the names (no full path) of the .obj or .lib files. If the function/class under test "lives" in a particular .obj (it will be named after the respective .cpp) file and you don't add that .obj you'll get linker errors (that's your error right there)
    • Properties -> Configuration Properties, Linker, General, Additional Library Directories : Add the directory path of the .obj or .lib files.
    • Properties -> Configuration Properties, VC++ Directories, Include Directories : Add the header(s) directory(ies) of the project under test.

    Basically, every case is covered in msdn