How to Configure GoogleMock in Visual Studio 2017 After Already Installing GoogleTest? - visual-studio-2017

I installed the Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn package into my VS 2017 application solution. This was accomplished by adding a new GoogleTest project to my solution via "Add New Project/Other Languages/C++/Test/Google Test".
The testing works well, but now I am ready to try some mocking with gmock. So, I installed googlemock.v140.windesktop.static.rt-dyn via NuGet, but I have no idea of how to get it integrated into my test project.
My packages.config looks like this:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="googlemock.v140.windesktop.static.rt-dyn" version="1.7.0.1" targetFramework="native" />
<package id="Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn" version="1.8.0" targetFramework="native" />
</packages>
... but there are no external dependency header files or .lib files to link to as far as I can see. I don't know where to go from here. :-)
P.S. I have posted questions about GoogleTest on Microsoft's C++ forum, but they will not answer these types of questions about GoogleTest even though it was installed via Visual Studio.

There are some bad Google Test / Google Mock packages available on NuGet, such as the one referenced by this question. The one you want is the gmock package authored by Google Inc (version v1.8.1 as of this writing).
Once this package has been installed, your project's packages.config should look like:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="gmock" version="1.8.1" targetFramework="native" />
</packages>
And you can begin using GMock simply by adding
#include "gmock\gmock.h"
as mentioned in the documentation.

TLDR; The solution for me was renaming the include folder provided with Google Mock package
from:
packages\googlemock.v140.windesktop.static.rt-dyn.1.7.0.1\build\native\include\gtest
to
packages\googlemock.v140.windesktop.static.rt-dyn.1.7.0.1\build\native\include\gmock
I've the exact same situation here (same packages version), but I've solved in another way (since the solution from #R.Evans didn't work).
I've noticed that opening the Google Mock package there is a folder called gtest instead of gmock, and this name is hiding Google Test package. Uninstalling one, you can see the other one in the include path.
Moreover all Google Mock headers are using gmock as main path for their headers, so with the header folder called gtest Visual Studio is reporting tons of errors even in the Google Mock source code.

I stumbled upon an answer to my own question. After installing gmock via NuGet, I tried keying #include "gmock/gmock.h" in my test project's .cpp just under the #include "gtest/gtest.h". That did not work. I looked in the "External Dependencies" folder for any reference to gmock but did not find one. I was stumped until I replaced #include "gmock/gmock.h" with #include "gtest/gmock.h". There were no errors generated at that point. I looks like I am on my way to doing some google type mocks.

Related

How to use GoogleMock in Visual Studio?

This is going to be a self-answered, FAQ-style question. See answer below.
With Visual Studio 2017/2019 it is really easy to set up a new Google Test project and start writing tests (as long as you don't mind using older versions of GoogleTest versions anyway).
But what about using GoogleMock as well? You would think that since Google combined gtest/gmock some time ago that this would just work. Just #include "gmock/gmock.h" and mock away. But no, the GoogleTest NuGet package that is automatically added by the template does not include the gmock folder at all.
Trying to add a second GoogleMock NuGet package causes multiple problems, such as mismatched gtest/gmock versions, overlapping include paths, etc.
Replacing the Microsoft GoogleTest NuGet package with the one from Google causes a link error:
MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
So what is the current recommended (and least painful) way to set up GoogleTest/GoogleMock in Visual Studio? Tests should be able to be discovered, run, and debugged via the Test Explorer.
I've found two ways to set this up: Either compile the whole GoogleTest framework directly into each of the test projects, or create a library project to hold it. Using a library will give faster build times, but you'll need to make sure that compile/link options are the same on the library and the test projects.
Option 1: Compiling GoogleTest Directly in the Test Project
Create a new project from the Google Test template. Instructions here if needed.
Uninstall the Microsoft.googletest.v140.windesktop.msvcstl.static.rt-static NuGet package.
Install the latest gmock NuGet package from Google (currently v1.10.0).
Add the file gtest_main.cc to the project. It should be in ..\packages\gmock.1.10.0\lib\native\src\gtest\src\
At this point the project should look something like this (if it doesn't, try Unloading and Reloading the project):
The final configuration step is to disable use of Precompiled Headers for the three Google .cc files (Important: Notice the empty fields too).
Option 2: Using GoogleTest in a Static Library Project
Create a new project from the Static Library (C++) template. Instructions here if needed.
Delete all generated .h/.cpp files (pch.h, pch.cpp, framework.h, <ProjectName>.cpp, etc)
Install the latest gmock NuGet package from Google (currently v1.10.0).
Disable use of Precompiled Headers for the library project (see related pic above).
Create a new project from the Google Test template. Instructions here if needed.
Uninstall the Microsoft.googletest.v140.windesktop.msvcstl.static.rt-static NuGet package.
Add the file gtest_main.cc to the project. It should be in ..\packages\gmock.1.10.0\lib\native\src\gtest\src\
Disable use of Precompiled Headers for gtest_main.cc (see related pic above).
Add the library project to the test project's Project References.
Add ..\packages\gmock.1.10.0\lib\native\include\ to the test project's Include Directories under VC++ Directories
The solution structure should now look something like this:
Writing the Tests
Either way, you are now ready to start writing tests using GoogleMock. Add #include "gmock/gmock.h" to the pch.h file:
//
// pch.h
// Header for standard system include files.
//
#pragma once
#include "gtest/gtest.h"
#include "gmock/gmock.h"
Open the generated Test.cpp file and try it.
#include "pch.h"
class MockTest {
public:
MOCK_METHOD(void, SomeMethod, ());
};
TEST(TestCaseName, TestName) {
MockTest mock;
EXPECT_CALL(mock, SomeMethod);
mock.SomeMethod();
EXPECT_EQ(1, 1);
EXPECT_TRUE(true);
}

Nuget not installing in Azure DevOps build

I'm trying to stop archiving into code source repository anything that comes from a Nuget package. For most, since they only have dlls, it works.
My problem is that I have one Nuget with a Content folder in the nupkg. When building using VS2017, everything gets installed correctly but when doing the same thing using the build server (Azure DevOps Pipeline), I get
Error MSB3030: Could not copy the file "....." because it was not found.
The pipeline agent version is v.2.122.1 according to Capabilities.
NuGet Task logs version 0.2.31 using NuGet 3.3.0.212 with MsBuild 14.0.
I see the nupkg files in the packages folder but the Content folder items don't get copied.
Is there anything special I need to add to my csproj or any additional steps I should add to my build definition?
Is there anything special I need to add to my csproj or any additional steps I should add to my build definition?
You need change your csproj file to link the files from content package instead of add the files into the project. Since you are trying to stop archiving into code source repository anything that comes from a Nuget package, then add a nuget restore task to your build definition.
As test, I create a content package and add a copy event to copy file from the content package:
After installing that package, following content will be add to the project file .csproj and packages.config:
.csproj:
<ItemGroup>
<Content Include="resources\Test.txt" />
<Content Include="resources\Test2.txt" />
</ItemGroup>
Packages.config:
<packages>
<package id="TestContentPackage" version="1.0.0" targetFramework="net461" />
</packages>
Then we change the .csproj file to link the files from content package:
<ItemGroup>
<Content Include="..\Packages\TestContentPackage.1.0.0\content\resources\Test.txt" />
<Content Include="..\Packages\TestContentPackage.1.0.0\content\resources\Test2.txt" />
</ItemGroup>
After change, the files from content package are not added to the project, just link to the project:
To verify the copy command, I add following copy command in the build event:
if not exist $(ProjectDir)TestFolder mkdir $(ProjectDir)TestFolder
xcopy /y "$(SolutionDir)Packages\TestContentPackage.1.0.0\content\resources\Test2.txt" "$(ProjectDir)TestFolder"
All the things work fine on my local.
Then I build this project with Azure devops, you need add the nuget restore task, otherwise, Visual Studio/MSBuild could not find the files from the packages. And it also work fine.
Note: If you want to copy the files from the content folder, you need copy it from the packages folder.
Hope this helps.

Nuget Package Installs Via Console But No Reference Added For C++ Project

I am trying to install the NuGet package detailed on this webpage: https://learn.microsoft.com/en-us/azure/storage/storage-c-plus-plus-how-to-use-blobs
For reference, the instructions are:
Windows: In Visual Studio, click Tools > NuGet Package Manager > Package Manager Console. Type the following command into the NuGet Package Manager console and press ENTER.
This works... kind of. The package is downloaded locally, but the reference isn't being added to my project. When I install the package manually or from the PM Console, no reference is added.
Instructions I've been able to find (such as here) just insist that the references will be automagically added. Which is great when it works, but it provides no debug reference point or how to add them manually.
Are there other options I'm missing?
Thanks.
the reference isn't being added to my project. When I install the package manually or from the PM Console, no reference is added
That is because NuGet cannot directly add references to native projects, the ‘native’ target framework is not recognized within the \lib folder.
For the detail information, you can refer to the Support for Native Projects.
Besides, after installed the wastorage package, you will notice that the blob.h and storage_account.h were added to External Dependencies:
Then you can add the following include statements to the top of the C++ file successfully where you want to use the Azure storage APIs to access blobs:
#include <was/storage_account.h>
#include <was/blob.h>
Update:
According to the JuniorIncanter comment, add the .targets, and .props file in the .vcxproj:
<ImportGroup Label="ExtensionTargets">
<Import Project="..\packages\cpprestsdk.v120.windesktop.msvcstl.dyn.rt-dyn.2.9.1\build\native\cpprestsdk.v120.windesktop.msvcstl.dyn.rt-dyn.targets" Condition="Exists('..\packages\cpprestsdk.v120.windesktop.msvcstl.dyn.rt-dyn.2.9.1\build\native\cpprestsdk.v120.windesktop.msvcstl.dyn.rt-dyn.targets')" />
<Import Project="..\packages\cpprestsdk.v140.windesktop.msvcstl.dyn.rt-dyn.2.9.1\build\native\cpprestsdk.v140.windesktop.msvcstl.dyn.rt-dyn.targets" Condition="Exists('..\packages\cpprestsdk.v140.windesktop.msvcstl.dyn.rt-dyn.2.9.1\build\native\cpprestsdk.v140.windesktop.msvcstl.dyn.rt-dyn.targets')" />
<Import Project="..\packages\wastorage.v120.3.0.0\build\native\wastorage.v120.targets" Condition="Exists('..\packages\wastorage.v120.3.0.0\build\native\wastorage.v120.targets')" />
<Import Project="..\packages\wastorage.v140.3.0.0\build\native\wastorage.v140.targets" Condition="Exists('..\packages\wastorage.v140.3.0.0\build\native\wastorage.v140.targets')" />
For those that stumble onto the question, there is no working solution. The issue arose because I was creating a UAP app, and the given Nuget package does not have UAP support. At that point, in order to continue I must download the source code for the Nuget package and recompile it with the -ZW flag.
See here: https://msdn.microsoft.com/en-us/library/mt186162.aspx
If you have source code for the DLL or static library, you can recompile with /ZW as a UWP project.

VS2012 Unit Tests: How to change the location of the TestResults folder

I have all my Unit Test project in a folder under my Solution Folder and would like to have the TestResults folder in the same folder as the Test projects instead in the solution directory.
I have found that this could be done via the test seeting file:
How to specify the location for the unit test results in VS 2010?
but I also read, that with VS2012 you should no longer use tests settings files. Actually VS2012 does not create one.
Is there another way?
To specify a different location for the "TestSettings" folder, add a .runsettings to your solution as explained in the Visual Studio documentation: http://msdn.microsoft.com/en-us/library/vstudio/jj635153.aspx
My .runsettings file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<ResultsDirectory>.\Visual Studio Test Results</ResultsDirectory>
</RunConfiguration>
</RunSettings>
As far as I could tell, however, the ResultsDirectory location is not relative to the solution folder (like the example file from the doc suggests), but rather relative to the location of the .runsettings file itself. Also note that Visual Studio macros like $(SolutionDir) are not expanded here.
All in all, .runsettings files are not related to a particular project or solution.
The reason why they recommend using .runsettings files instead of .testsettings in newer version of Visual Studio is also found in the documentation: http://msdn.microsoft.com/en-us/library/vstudio/ee256991.aspx
If you use a .testsettings file, the MSTest test framework will be
used to run your tests. This runs more slowly and does not allow you
to run tests from third-party test frameworks.
You can create a small RunSettings file which looks like
<RunSettings>
<RunConfiguration>
<ResultsDirectory>e:\myResultsFolder</ResultsDirectory>
</RunConfiguration>
</RunSettings>
Select this setting file via top level menu Test->TestSettings->"Select Test Settings" before running your tests.
You can find more detail on http://msdn.microsoft.com/en-us/library/jj635153.aspx.

Using NLog 2 with Silverlight 5

Has anyone ever used NLog 2 with SL5?
I cannot find DLLs for SL5 on the NLog homepage. When I try to reference the SL4 DLLs, VS 2010 says the following:
Why? I thought it is possible to reference SL4 DLLs in a SL5 project (but not the other way round)?
So I checked out NLog sources, opened its SL4 project, changed the target version from SL4 to SL5, Clean, Build and referenced the newly created DLL. While this seems to work it just doesn't feel right.
Any better ideas?
Update: Adding a reference to the original NLog 2 DLLs built for SL4 by directly editing the .csproj file of my SL5 project seems to do the job as well. I'm confused...
<Reference Include="NLog">
<HintPath>..\Libraries\NLog\NLog.dll</HintPath>
</Reference>