6

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!

stijn
  • 34,664
  • 13
  • 111
  • 163
tommed
  • 1,521
  • 2
  • 19
  • 33
  • @tommed, can you build / run this from a Visual Studio Command prompt? The issue is probably because msbuild in VS 2017 is app local, which means there are special env variables that are used to determine the location of msbuild, the .targets and class libraries that are used. – Michael Braude Nov 28 '16 at 18:54
  • @MichaelBraude thanks for that. I have also tried the same in the _Developer Command Prompt_ and the _MSBuild Command Prompt_ and get the same error - nice thought though! – tommed Nov 28 '16 at 22:37
  • Although not about NET Core i'm posting this as a comment: VS2017 Full NET 4.5 framework here, same problem: no unit tests to be found. I had to uninstall and reinstall `xunit.runner.visualstudio` from one of my projects using Nuget. Then rebuild solution, and tests were found. Hope this helps. – Sharky Jul 08 '17 at 18:33

3 Answers3

7

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.

Brad Wilson
  • 67,914
  • 9
  • 74
  • 83
6

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.

Dale Alleshouse
  • 1,627
  • 2
  • 17
  • 24
  • 2
    Appreciate you supplying this information. I have also come to the conclusion that `dotnet-test-xunit` breaks VS2017 - the `xunit.runner.visualstudio` nuget package is already installed, but there is no way to run the cli tests. – tommed Nov 28 '16 at 22:35
2

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.

nohwnd
  • 826
  • 7
  • 13
  • That fixed my issues related to testing with xUnit. Visual Studio 2017RC has surprisingly hight amount of errors introduced to project and its tests duo out-of-the-box. – Hekkaryk Feb 08 '17 at 14:14