Setting up unit testing for C++ in Visual Studio 2015 - c++

I'm having trouble trying to set up unit testing. My test class is very simple and looks like this:
TEST_CLASS(BlahSpec)
{
public:
TEST_METHOD(blahFunc)
{
int res = doSomething(); // this is what I'm testing
Assert::AreEqual (42, res, L"Blah", LINE_INFO());
}
};
The project compiles and runs fine. However when I choose TEST > Run > All Tests, I get the following error:
Cannot discover test extensions installed by NuGet. The NuGet service is not
available. Tests may not be found due to missing test adapter extensions
Do I have to download a NuGet package to get unit testing for C++? If so, what NuGet package should I download? I don't want any fancy third-party package, I just need the Microsoft default thing. However, the NuGet extension manager doesn't show me anything that seems to come from Microsoft.
EDIT: I've picked a NuGet package at random, NUnit3, and installed it. Now I get the following errors:
Error: Unable to get runner for this assembly. Check installation, including any extensions.
BadImageFormatException: Format of the executable (.exe) or library (.dll) is invalid.
Assembly not supported: C:\Develop\test-project\projects\Debug\UnitTestApp1\UnitTestApp1.exe
As it appears, NUnit3 does not seem the right choice for C++ native unit tests. Which NuGet package should I pick?

Related

Unable to add Google Test NuGet package to a Visual Studio Linux C++ project

I have a Linux C++ project in Visual Studio 2017 which contains unit tests, and I'm trying to add the Google test framework to it. I have added the gtest NuGEt package to the project, and in every file that uses this, I have a #include "gtest/gtest.h". This works fine in a Windows-based unit test project, but in the Linux C++ project (all else being equal as far as I can tell), the NuGet package doesn't seem to be able to work, and when I compile I get the following error :
gtest/gtest.h: No such file or directory.
Does anyone know why this doesn't work?
That's because VS Linux projects are compiled and executed on a connected Linux machine (see Tools / Options / Connection Manager). Which in your case does not have Gtest installed.

Why do I get a "No test adapters are referenced by this solution" error message?

When I start a Live Unit Tests session on my solution in visual studio 2017, I get the following message:
No test adapters are referenced by this solution. If you have a test
project, add a NuGet reference to a test adapter corresponding to the
test framework used in order to run the tests. If you already have the
required NuGet reference in your test project, performing a NuGet
restore may resolve the issue.
What can I do to remedy this situation?
As the message implies, you need to install some NuGet packages, one for the testing framework and one for the visual studio test runner. If you are upgrading an old solution using MSTest, you first need to remove the reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework.
Then you need to open the Package Manager Console (under Tools-> Nuget Package Manager -> Package Manager Console). Select your unit test project(s) and run the following commands:
Install-Package MSTest.TestFramework
Install-Package MSTest.TestAdapter
If you are using a different test framework, such as XUnit or NUnit, you have to install either XUnit and xunit.runner.visualstudio, or NUnit and NUnit3TestAdapter
I created a brand new project in VisualStudio 2017 and was getting the same error message until I installed xunit.runner.visualstudio NuGet package. Follow these instructions on the Xunit getting started webpage (https://xunit.github.io/docs/getting-started/netfx/visual-studio)
I was able to see the tests in Test Explorer but they were being ignored.
I found and deleted a Local.testsettings file in the solution folder and it fixed the problem (I had earlier updated the nuget packages as the other answer suggests but that was not enough in my case).
I use nunit, i have project from 2012. I installed NUnit3TestAdapter as suggested but it didnt help. Turned out my nuit vesion 2.6.1 was too old even for NUnit3TestAdapter which job is to work with version 3 nunit. So i selected in nuget terminal my project with tests, ran "Install-Package nunit", it updated NUnit.2.6.1 up to NUnit.3.13.3 and tests now can run.

Can't get XUnit tests working with Visual Studio 2017 RC

For the life of me I can't get unit testing working in Visual Studio 2017 from the new msbuild-based netcoreapp1.0 xunit project template.
The requirement is for unit tests to work both inside Visual Studio (for the devs) and from dotnet test on the CLI for the automated build process however, I can't get either working consistently.
Here is what I have tried:
In an existing solution, create a new project and select .NET Core > xUnit Test Project.
Build project from Visual Studio, default test appears and runs successfully, now run dotnet test from powershell prompt, get:
> dotnet test
Test run for D:\...\bin\Debug\netcoreapp1.0\MyProj.dll(.NETCoreApp,Version=v1.0)
dotnet exec needs a managed .dll or .exe extension. The application specified was 'C:\Program'
Or dotnet test with csproj file:
> dotnet test MyProject.csproj
(same error as above)
> dotnet test ..\MySolution.sln
Couldn't find a project to run test from. Ensure a project exists in D:\...
Or pass the path to the project
If I add the xunit.runner.console or xunit.runner.msbuild nuget packages, it stops the unit tests working from inside Visual Studio.
How do I get both working at the same time?
Thanks!
The bug you're hitting is present in Preview 3 and fixed in Preview 4. They didn't escape the command line when executing it, and since dotnet.exe is installed into C:\Program Files\dotnet by default, it always fails.
If you want to continue to use Preview 3, the simplest work-around is to edit your system PATH environment variable, and replace C:\Program Files\dotnet with C:\Progra~1\dotnet.
I know this isn't a very good answer, but dotnet-test-xunit only support project.json files. VS 2017 forces you to switch to csproj files.
I found this on xunit twitter feed:
If you're trying use #xunit in VS2017 RC w/ .NET Core, remove dotnet-test-xunit and use xunit.runner.visualstudio 2.2 beta 4 instead.
With the latest RC.3 I was having issues with the tests not being discovered, and found out that when you run the built-in Test Explorer it says in the output that Microsoft.DotNet.InternalAbstractions 1.0.0 is missing. This was also issue in the previous versions of .NET Core, and the solution is the same, install the package from Nuget.

xUnit Tests no longer finding tests in VS 2013

If this is a duplicate please let me know and I'll promptly close this question out.
I've got a small issue with xUnit in Visual Studio 2013 Update 3 (12.0.30723.00). Some time ago, apparently, my tests stopped showing up in the Test Explorer around a recent update to xUnit's test runner.
I have verified the proper extension is installed in Visual Studio (v0.99.8)
I have also verified my project references a supported version of xUnit (1.9.2.1705).
Here is a test I just wrote:
using Xunit;
namespace Core.Test
{
public class CoreTests
{
[Fact]
public void Test()
{
Assert.True(true);
}
}
}
When I Compile the solution, the Test Explorer does not find any tests to run.
My solution is currently set to x86 - DEBUG build. Changing this to x64 or AnyCPU (Debug/Release) does not change this.
Now, if I upgrade xunit from 1.9.2.1705 to xunit 2.0 b4 build 2738, the test runner finds all of my tests just fine.
Does anybody else have the same issue as me and if so how do I resolve it?
To Be Safe:
First, make sure that you're using the package instead of the extension. Uninstall the extension by going to Tools -> Extensions and Updates then scroll to the bottom and uninstall it.
Now right-click the solution > Manage Nuget Packages For Solution and install the xUnit runners package.
My Suggestion to Solve Your Issue:
If the solution is in the same directory as the project, this change just fixed a bug with tests not being found.
Otherwise, I've noticed that if you change your Projects directory (Tools > Projects and Solutions > General) to something other than the default (%userprofile%\Documents\Visual Studio 2013\Projects), the issue crops up. So try resetting your Projects directory to the default.

Why is the xUnit Runner not finding my tests

I have a xUnit.net Test as follows:
static class MyTestClass
{
[Fact]
static void MyTestMethod()
{
}
}
The xUnit plugin for VS 2012 says:
No tests found to run.
TestDriven.net runs it fine but mentions something about Ad hoc:
1 passed, 0 failed, 0 skipped (see 'Task List'), took 0.47 seconds (Ad hoc)
TeamCity, xunit.gui.exe and xunit.console.exe and Visual Studio also can't find TestMethod
(I've got xunit.runner.visualstudio installed and VS is seeing some tests.)
What gives?
TL;DR your Test Classes must be public (but your Test Methods can be private and/or static)
For reasons of efficiency, the xUnit authors have opted to not use BindingFlags.NonPublic when searching for Test Classes in the runner (the MSIL metadata tables don't index private(/internal) classes to the same degree hence there is a significant performance difference in the relative efficiency that Reflection can thus achieve).
As a result of the above, the fact that your class is private means it doesn't get picked up.
The fact that the Test Method is private and static is fine - xUnit by design since 1.0 has supported both those aspects.
Note that the Visual Studio xUnit Runner extension, xunit.console.exe (and the GUI), the xunit MSBuild task, Resharper and CodeRush are all consistent in honouring this (although arguably they [especially the latter two] could do more to flag when a Test Class (i.e. class [potentially indirectly] containing Fact-derived annoations) is private).
The reason TestDriven.net runs your test is that the Author of TestDriven.net has put great effort into making it Just Work. It internally uses a special Test Runner wrapper/shim (termed the Adhoc Runner) to run your test. Be aware that the method is actually not being run via the xUnit.net runner and hence any attributes you put on your test that have side effects will not be triggered.
Notably NUnit (and I'm pretty sure MSTest) do use private reflection [and hence pick up tests in private classes] which is probably why it never seemed an important thing for you to worry about before.
Note: A side effect / trick enabled by this is that you can make a Test Class private as a quick way of Skipping all tests in a Test Class [and any nested classes]. (Sadly the cases on this planet of this being used unintentionally vastly outnumber the intentional cases of this though!)
This answer is for VS 2013, but the steps are essentially the same for VS 2012. This applies for running via ReSharper's unit test functionality.
Install the xUnit.net runner for Visual Studio 2013 (be careful running Visual Studio as an Administrator, or the tests may not run when running the IDE as a non-Admin):
a. From within Visual Studio 2013, go to Tools -> Extensions and Updates -> Online
b. Search for xUnit.net runner for Visual Studio 2012 and 2013
c. Then download (install) it. If upgrading to VS 2013 from VS 2012, it is suggested that this be uninstalled, and then re-installed.
d. Restart Visual Studio.
If ReSharper is installed, install the xUnit.net test runner plugin :
(NOTE: Since ReSharper 2016.1, xunit support is built in to ReSharper, meaning the xunit plugin is no longer required.)
a. In Visual Studio 2013, Navigate: Resharper -> Extension Manager.
b. On the left, select Online.
c. Search for “xunit.net”. Select the “xUnit.net Test Support”. Click Install.
d. Restart Visual Studio 2013.
“Clean” the solution
a. In the IDE, in Solution Explorer, right-click the solution, and choose “Clean”.
b. Re-compile.
c. Now, when right-clicking a [Fact] attribute, select Resharper’s “Run Unit Tests” (as opposed to the default “Run Tests”)
Troubleshooting running with XUnit:
If problems running the [Fact] tests with XUnit persist, it might
be necessary to manually remove the xUnit package from any/all of the
following folders (review content for the xunit DLLs, then delete
xUnit folder if found):
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\
C:\Users\<username>\AppData\Local\Microsoft\VisualStudio\12.0\Extensions\
As for ReSharper, try un-installing and re-installing the
xunitcontrib library (xUnit.net Test Support). I have noticed once
when un-installing, some error messages flashing past. I grabbed a
screen-shot at one point, and it listed:
Access to the path
C:\Users\<username>\AppData\Local\JetBrains\ReSharper\vAny\packages\xunitcontrib.1.3.0\ReSharper\v8.1\plugins\xunit.dll
is denied.
... and the same for the other DLLs in that directory
To resolve this, delete the C:\Users\<username>\AppData\Local\JetBrains\ReSharper\vAny\packages\xunitcontrib.1.3.0\ directory after uninstalling from Visual Studio, then run Visual Studio as a non-administrator, and re-install via ReSharper (Resharper -> Extension Manager)
From http://xunit.github.io/docs/getting-started-desktop.html#run-tests-visualstudio:
If you're having problems discovering or running tests, you may be a
victim of a corrupted runner cache inside Visual Studio. To clear this
cache, shut down all instances of Visual Studio, then delete the
folder %TEMP%\VisualStudioTestExplorerExtensions. Also make sure your
project is only linked against a single version of the Visual Studio
runner NuGet package (xunit.runner.visualstudio).
I had the same issue in VS2017 RC, .NET core 1.1 project. Updating xunit.runner worked for me,
Install-Package xunit.runner.visualstudio
(As referred to by #Kyle in the comments on the other answer) The same No tests found to run message can result from using NuGet to get xUnit.dll and ending up with version 2.0.0 (which is currently marked as prerelease as some core functionality like discovering of v1 tests etc. has yet to be implemented in that branch).
The resolution in this case is to select Stable Only versions (as opposed to Include Prerelease) in the NuGet package manager.
I've been having this issue with .NET Core for a while now where a test class or a test method is not being discovered. The following fix works for me:
Open a command prompt window.
Change to the project directory.
Build the project running the following command:
dotnet build
NOTE: Building from Visual Studio.NET will not work! <<<<<<<<<<< IMPORTANT!
Run the tests: Test --> Run --> Test All - CTRL+R +A (this will discover the new test(s) - but not run the the new test(s).
Run the tests again.
In my case, in order to see any tests, I had to complete the following steps:
(All installed through NuGet Package Manager)
Install xUnit v2.0.50727
Install xUnit.extensions v2.0.50727
Navigate to the following link and follow the steps outlined in the documentation: http://xunit.github.io/docs/running-tests-in-vs.html
I'm using Visual Studio 2013 Premium. (Resharper NOT installed)
For me, the combination of my test class and test method names were too long; xUnit appears to have some cap on this combination.
Shortening the name of just the test method allowed xUnit to discover that single test. Shortening the name of the entire class allowed xUnit to discover all tests in the class.
Threshold of class name + method name appears to be 172 characters.
My problem was that I updated xunit.runner.visualstudio to version 2.4.5. However, the project I am working for, is for .NET Standard 2.0. Therefore, I had to downgrade to version 2.4.3 of xunit.runner.visualstudio, since it supports ".NET 2.0 or later". But since version 2.4.4, ".NET Core 3.1 or later" is supported.