Search Results for

    Show / Hide Table of Contents

    .NET Core

    More information and getting started tutorials are available for NUnit and .NET Core targeting C#, F# and Visual Basic in the .NET Core documentation's Unit Testing in .NET Core page.

    The other information on this page is older documentation. If you follow the instructions in the Installation section your project will work with .NET Core.

    The test projects have to be .NET (Core) or .NET Framework; .NET Standard can't be used as a test project, since it can't be run on its own, but any code in a .NET Standard library can be tested from a .NET (Core) or .NET Framework test project.

    TL;DR

    Adding the adapter and Microsoft.NET.Test.Sdk version 17.8.0 or greater to your NUnit test projects will also enable the dotnet test command for .NET Core projects.

    Any tests using the new style CSPROJ format, either .NET Core or .NET 4.x, need to add a PackageReference to the NuGet package Microsoft.NET.Test.Sdk. Your test assemblies must also be .NET Core or .NET 4.x, not .NET Standard.

    You can create a new NUnit test project using dotnet new nunit. It will create an ItemGroup in the csproj file with the necessary references.

      <ItemGroup>
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
        <PackageReference Include="NUnit" Version="4.0.1" />
        <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
        <PackageReference Include="NUnit.Analyzers" Version="3.10.0" />
        <PackageReference Include="coverlet.collector" Version="6.0.0" />
      </ItemGroup>
    

    .NET Core test can be run on the command line with dotnet test, for example,

    From the solution root folder

      dotnet test
    

    or from the test project folder

      dotnet test
    

    Or you can specify the csproj file you want to test

      dotnet test .\test\NetCore10Tests\NetCore10Tests.csproj
    

    For a more complete walk-through, please see Testing .NET Core with NUnit in Visual Studio 2017

    Using the NUnit project templates

    The NUnit test project templates come included with dotnet.

    You can run dotnet new nunit to create an NUnit test project.

    FAQ

    Why can't my tests target .NET Standard

    Visual Studio and VSTest require that the tests target a specific platform. .NET Standard is like a Portable library in that it does not target any specific platform, but can run on any supported platform. Microsoft decided that your tests should be compiled to target a platform so they know which platform to run your tests under and you get the behavior you expect for the platform you are targeting. You can however target multiple platforms in your tests and compile and run each from the command line. It still only runs one platform from Visual Studio, but I would hope that is being worked on. I haven't tested 15.3 yet.

    It is similar to a console application, it cannot be .NET Standard, it must target a platform, .NET Core or .NET Framework.

    This limitation is the same for all test adapters including xUnit and MSTest2.

    My tests aren't showing up in Visual Studio 2022 or later

    • Are you using the NuGet adapter package?
    • Are you using version 4.5.0 or newer of the NuGet adapter package?
    • Do your tests target .NET Core or the full .NET Framework? (see above)
    • Have you added a Package Reference to Microsoft.NET.Test.Sdk?
    • Have you restarted Visual Studio? It is still a bit temperamental.
    • Are you doing integration testing with Asp.Net Core WebApplicationFactory and using minimal API, and it complains about missing testhost.deps.json? Then you're accessing the wrong Program class in your WebApplicationFactory. Fix it by adding a code line at the end of the Program.cs file, and verify that your WebApplicationFactory is actually using this Program class:
      public partial class Program { }
    

    The reason for this is that the generated/hidden Minimal API (Top Level statement) Program class is internal. By declaring the partial part, it is changed to public. See Microsoft docs for more info, and some alternatives.

    My tests multi-target .NET Core and .NET Framework, why can't I run both in Visual Studio

    This was a limitation in earlier versions of Visual Studio, but is fixed in Visual Studio 2022.

    If you must run an earlier version and have this issue, you can run specific tests using the --framework command line option of dotnet test

    How do I produce a test results file

    dotnet test can generate an NUnit3 test result file by adding a runsettings property. The property to add is TestOutputXml. This generation is done using the NUnit Engine report service, and produce the same result as the NUnit3-console. This is available through the NUnit3TestAdapter.

    You run it by adding the setting on the command line (or in a runsettings file):

    dotnet test  -- NUnit.TestOutputXml=yourfoldername
    

    The folder is relative to a base folder determined by the OutputXmlFolder settings, or you can use an absolute path. The latter is useful in CI scenarios.

    Alternatively, there is a 3rd party package, NUnitXml.TestLogger which also produces a NUnit3 xml format. Details for use see here. Note that this is a re-implementation of the NUnit3 format, and may differ.

    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0