I want to unit test my C++ project with Visual Studio. After adding the folders from my project as include path to my test project, I get linker errors when trying to compil
I just ran into the same problem and solved it differently, following this piece of Microsoft documentation: https://docs.microsoft.com/nl-nl/visualstudio/test/unit-testing-existing-cpp-applications-with-test-explorer?view=vs-2015&redirectedfrom=MSDN#objectRef
This solution is a bit cleaner, I think, as you don't end up with all the header files from your projects under test also showing up in your test project.
Lines from the documentation in italics, bold lines are my additions:
If the code under test does not export the functions that you want to test, you can add the output .obj or .lib file to the dependencies of the test project.
Create a C++ test project.
On the File menu, choose New, Project, Visual C++, Test, C++ Unit Test Project.
Make sure to add the projects that you want to test as References. If you forgot, you can also add these references later, in the Solution Explorer window.
In Solution Explorer, on the shortcut menu of the test project, choose Properties. The project properties window opens.
Choose Configuration Properties, Linker, Input, Additional Dependencies.
Choose Edit, and add the names of the .obj or .lib files. Do not use the full path names.
For me, there were only .obj files. I found them in the Intermediates folder, of which there are multiple versions, one for each combination of Solution Platform (x86 or x64) and Solution Configuration (Debug or Release). If you have a lot of .obj (or .lib) files, I found it convenient to open a terminal and run ls (or dir) to get the filenames, edit out the .log, .txt and .pdb filenames that I didn't need, and copy paste the list of .obj filenames into Visual Studio. For convenient editing, note that you can click on the dropdown arrow on the right hand side of the Additional Dependencies entry field and click on edit.
Choose Configuration Properties, Linker, General, Additional Library Directories.
Choose Edit, and add the directory path of the .obj or .lib files. The path is typically within the build folder of the project under test.
For me, I only had .obj files. I found them in the Intermediates folder, of which there are multiple versions, one for each combination of Solution Platform (x86 or x64) and Solution Configuration (Debug or Release). A convenient macro for setting this path for all 4 combinations is this: $(SolutionDir)bin\$(PlatformTarget)\$(IntermediateOutputPath)Intermediates. You might have to take out the bin\ part, as I have also seen the output folders set up directly in the Solution Directory, instead of in a separate binary folder.
Choose Configuration Properties, VC++ Directories, Include Directories.
Choose Edit, and then add the header directory of the project under test.
For this directory, it's okay to point directly to folders that are part of the project under test. There is no need to copy the header files over to the test project. It's also okay if this directory contains more than just the headers. For example, my source folder contains both the .h files and the .cpp files.
After following these steps, I was able to include the header files in my test code by simply writing:
#include "SomeHeaderName.h"
So, without any need to specify the folders that the header files are in.
Make sure to clean and rebuild your entire solution. Then, your tests should be able to access the functionality of the projects that you are testing.
https://www.codeproject.com/Tips/1085171/How-To-Do-Unit-Testing-with-Cplusplus-in-Visual-St
I found there that you have to add your .h and .cpp files as existing files also to the test project. That is left out on the official documentation or I missed it.
Now it works!
I believe the chosen answer is not correct. Tests typically should be run in their environment; therefore, they should not access the implementation (.cpp)
When you create a separate Test Project on Visual Studio (VS 2017) you need to create a reference to the project you want to test (right-click test project -> Add -> Reference -> tick projects): Add a reference
If you see some linker errors, right-click from the Unit test project: Project->Linker->Input->Additional Dependencies->Edit then add the path to your .obj files. You could try something like "$(SolutionDir)ConsoleApplication1\$(IntDir)*.obj" where ConsoleApplication1 is the target project name.
Path to .obj
Initially, I only wanted to put a comment on Cornelis' post, but I couldn't.