Run NUnit tests in .NET Core

2020-06-07 01:21发布

I am trying to run unit tests for my C# project with .NET Core. I am using a Docker container for the runtime.

Dockerfile

FROM microsoft/dotnet:0.0.1-alpha
RUN mkdir /src
WORKDIR /src
ADD . /src
RUN dotnet restore

"NUnit" and "NUnit.Runners" have been added into project.json

"version": "1.0.0-*",
"compilationOptions": {
    "emitEntryPoint": true
},

"dependencies": {
    "NETStandard.Library": "1.0.0-rc2-23811",
    "NUnit": "3.2.0",
    "NUnit.Runners": "3.2.0"
},
"frameworks": {
    "dnxcore50": { }
}

Run dotnet restore successfully with the following output

...
log  : Installing NUnit.ConsoleRunner 3.2.0.
log  : Installing NUnit.Extension.NUnitV2ResultWriter 3.2.0.
log  : Installing NUnit.Extension.NUnitV2Driver 3.2.0.
log  : Installing NUnit.Extension.VSProjectLoader 3.2.0.
log  : Installing NUnit.Extension.NUnitProjectLoader 3.2.0.
log  : Installing NUnit.Runners 3.2.0.
info : Committing restore...
log  : Restore completed in 4352ms.

I tried to run the tests with:

dotnet nunit

dotnet nunit-console

But it doesn't work.

How am I going to call the runner? Or is there another unit testing framework that works with the current version of .NET Core?

3条回答
手持菜刀,她持情操
2楼-- · 2020-06-07 01:59

Update 4: The NUnit3TestAdapter v3.8 has been released, so it is no longer alpha.

Update 3: With NUnit3TestAdapter v3.8.0-alpha1 it is possible now to run the tests using dotnet test command. You just need to have these dependencies in your test project:

<PackageReference Include="nunit" Version="3.7.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.8.0-*" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.*" />

You can try it out!

Update 2: Visual Studio 2017 and the move from project.json to csproj made the dotnet-test-nunit test adapter obsolete, so we needed to release another updated adapter to run .NET Core tests. Please see Testing .NET Core with NUnit in Visual Studio 2017 if you are using VS2017 and the new .NET Core tooling. See the update below if you are using project.json.

Update: NUnit now has support for dotnet test, so you no longer have to use NUnitLite. See testing .NET Core RC2 and ASP.NET Core RC2 using NUnit 3.


NUnit console (and the underlying NUnit Engine) do not support running unit tests against .NET core yet. Hopefully we will get that support in NUnit 3.4.

In the meantime, you can use NUnitLite to switch your tests to a self-executing test runner.

I wrote a blog post on the process at Testing .NET Core using NUnit 3. A quick summary is;

  1. Create a .NET Core Console application for your test project.
  2. Reference NUnit and NUnitLite from your test project. You do not need the runner.
  3. Modify main() to execute the unit tests.

It should look like this;

using NUnitLite;
using System;
using System.Reflection;

namespace MyDnxProject.Test
{
  public class Program
  {
    public int Main(string[] args)
    {
      var writter = new ExtendedTextWrapper(Console.Out);
      new AutoRun(typeof(Program).GetTypeInfo().Assembly).Execute(args, writter, Console.In);
    }
  }
}

For more complete information, see my blog post.

查看更多
▲ chillily
3楼-- · 2020-06-07 02:15

I did as Rob-Prouse suggested, with minor changes. It finally works now.

using System;
using System.Reflection;
using NUnitLite;
using NUnit.Common;

........

public class Program
{
    public static void Main(string[] args)
    {

        var writter = new ExtendedTextWrapper(Console.Out);
         new AutoRun(typeof(Program).GetTypeInfo().Assembly).Execute(args, writter, Console.In);

    }
}
查看更多
劳资没心,怎么记你
4楼-- · 2020-06-07 02:18

Some tweaks to the answer, to make it compile.

using System;
using System.Reflection;
using NUnit.Common;
using NUnitLite;

namespace NetMQ.Tests
{
    public static class Program
    {
        private static int Main(string[] args)
        {
            using (var writer = new ExtendedTextWrapper(Console.Out))
            {
                return new AutoRun(Assembly.GetExecutingAssembly())
                    .Execute(args, writer, Console.In);
            }
        }
    }
}

Note that you may also have to convert your project from a class library to a console application.

查看更多
登录 后发表回答