If you can't get your solution to compile, for example by receiving an unresolved externals error, take a look at the answer section and recreate the steps listed there.
CodePudding user response:
Our example header:
#pragma once
#include <string>
std::string testfunc();
Our example sourcefile:
#include "to_test.h"
std::string testfunc()
{
return "test worked";
}
After creating our example project, we wanna check some things on our list beforehand.
- add google test adapter via visual studio installer (under individual components, search for "google")
- right click our test project inside of our solution and click on "manage NuGet Packages", switch to the Browse tab and search for "gtest" and add it. It looks sorta like this:
- we then want to add a unit test project to our solution. we right-click our solution in the solution explorer and choose add->new project. We search for "google" and add the one things that pops up which is named "Google Test". For a start we keep every setting on default, except for the path which we are going to change from the parent-folder of the solution to the folder of the project we are going to test (bacially just one depth deeper). We will open our test.cpp and add it somewhat like this: (note: the #include of your custom header shouldn't be copy-pasted to make sure its the right path in your case)
#include "pch.h"
#include "../to_test.h"
TEST(test, TestName)
{
//This Test will work
EXPECT_TRUE(testfunc() == "test worked");
//This Test will fail
EXPECT_TRUE(testfunc() == "test not worked");
}
- now for the configuration: right-click on your test-project and open the properties.
- under VC Directories, add the location of your header files under the "Include Directories"
- under Linker->General, add the Debug-Folder of your Project which is to be tested under the "Addtional Library Directories"
- under Linker->Input simple add the name of your headerfile without the filetype to the "Additional Dependencies" in our case that would be "to_test" (without the quotes)
We can then right-click our solution and rebuild it. After that we can choose our GTest-1 Project as the Startproject via right-clicking it and then debug it as usual. The Terminal popping up should look sorta like this:
DISCLAIMER: This definietly isn't the only way to do this.. if someone cares to correct me i would highly appreciate it :)