1

Visual Studio 2019 is not detecting or discovering NUNit nor MSTest unit tests at all. I installed it fresh just a week ago. The MS guide here https://learn.microsoft.com/en-us/visualstudio/test/getting-started-with-unit-testing?view=vs-2019&tabs=mstest does not work.

A few threads hold possible solutions, but none that I've tried helped have helped. I'm new to C# so much of the steps have taken a long time to figure out, but while my application is progressing nicely I really want to work and learn in a TDD style. Even if a create a new blank MSTest project, with no application code or libs in the solution at all, the example/template project does not work, so I'm missing something big someplace. (I have .NET Core SDKs installed - my intent is to target macOS and linux at a future point.)

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <IsPackable>false</IsPackable>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="nunit" Version="3.13.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
  </ItemGroup>
</Project>

Threads I have tried: Tests not running in Test Explorer Why will Visual Studio 2019 will not run my unit tests? Visual Studio 2019 Test Explorer puts all tests under "Not Run Tests" If I create a project targeting .NET 4.7 all is good, it's when I want to target Core that I'm unstuck, if that helps.

Stuck again I had to install a component called "NUnit 3", https://marketplace.visualstudio.com/items?itemName=NUnitDevelopers.NUnitTemplatesforVisualStudio worked for a moment... but when I added a testcase, it all broke again.

Did the nuget package updates, still not working.

I cloned. from git this project https://github.com/dotnet/samples/tree/master/core/getting-started/unit-testing-using-nunit And it does not work either.

I then re-installed 2019, and emailed my project to a friend, who merely removed the nunit nuget modules, added them back and then it worked for him, but the project he sent me back did not work. Module versions unchanged

I opened the same project in the Microsoft developer VM/iso image and the project works just fine. so it's my environment that is incompatible with nunit somehow. Is there a way to see some traces?

Changed the installation drive from D: to C: I get this error now

Testhost process exited with error: A fatal error occurred, the required library hostfxr.dll could not be found.
If this is a self-contained application, that library should exist in [C:\Users\zapho\src\c#\tutorials\working\ConsoleApp1\ConsoleApp1\NUnit.Tests2\bin\Debug\netcoreapp3.1\].
If this is a framework-dependent application, install the runtime in the default location [C:\Program Files\dotnet] or use the DOTNET_ROOT environment variable to specify the runtime location.
. Please check the diagnostic logs for more information.
Testhost process exited with error: A fatal error occurred, the required library hostfxr.dll could not be found.
If this is a self-contained application, that library should exist in [C:\Users\zapho\src\c#\tutorials\working\ConsoleApp1\ConsoleApp1\NUnit.Tests2\bin\Debug\netcoreapp3.1\].
If this is a framework-dependent application, install the runtime in the default location [C:\Program Files\dotnet] or use the DOTNET_ROOT environment variable to specify the runtime location.
. Please check the diagnostic logs for more information.
  • 1
    You tagged both NUnit and MsTest, although the page you linked and the references you have all relate to MsTest. Depending on whether you intend to use MsTest, NUnit or xUnit, you will have to do slightly different things, so please focus the question on what you want to use. – Charlie Jan 05 '21 at 00:30
  • Thanks. I do prefer NUnit - but since I'm not familiar with CORE yet, I'm happy to use MsTest if NUnit is in fact incompatible with CORE. I tried to set up NUnit initially, the last time I used NUnit must have been over 6 years ago. Falling back to .NET 4 on Windows is least preferred. –  Jan 05 '21 at 18:24
  • 1
    it's just that some of us (well me) only answer questions on topics we know something about. For example, I answer questions on NUnit, but skip questions on MsTest. NUnit works fine on .NET Core but you'd have to focus your question and sample code on what you have tried that didn't work in order for me or anyone else to answer. You probably have enough info for an mstest-related answer so I hope someone will give it to you. – Charlie Jan 06 '21 at 23:40
  • That does help Charlie, it means I should keep trying. So the latest NUnit should work with latest Core, I'm going to try set up a new project and capture the steps more explicitly with package versions or just push it into github, pretty sure someone will tell me what I got wrong eventually. At least if things work on my 3rd or fourth attempt I can answer my own question. –  Jan 07 '21 at 16:24
  • You are probaly using VS 2019 16.9+, so the NuGet package references in this project must be upgraded before things start to work. – Lex Li Jan 08 '21 at 23:40
  • Ran nuget package update, and it foudn some other packages to update - made no difference. About to re-install VS. This should not happen I installed 2019 community 2 weeks ago. –  Jan 09 '21 at 14:15

2 Answers2

4

You need to add the Microsoft.NET.Test.Sdk NuGet package to your solution to run tests in Visual Studio.

Modify your project file,

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <IsPackable>false</IsPackable>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="nunit" Version="3.13.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
  </ItemGroup>
</Project>
Rob Prouse
  • 22,161
  • 4
  • 69
  • 89
0

Fixed it by eventually setting environment variable

DOTNET_ROOT=D:\Program Files\dotnet\

as per an answer https://stackoverflow.com/a/61453119/337598