在dotnet核心中运行NUnit测试

时间:2021-08-20 15:40:55

I am trying to run unit tests for my c# project with dotnet core. I am using docker container for runtime.

我正在为我的c#项目运行单元测试。我在运行时使用docker容器。

Dockerfile

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

“NUnit”和“NUnit。已将跑步者添加到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

使用以下输出成功运行dotnet恢复

...
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.

tried to run the tests with

尝试运行测试

dotnet nunit

dotnet nunit

dotnet nunit-console

dotnet nunit-console

Not work.

不工作。

So my questions is how am I going to call the runner? Or can someone suggest another unit testing framework that works with current version of dotnet core?

我的问题是,我该怎么称呼跑步者?或者是否有人建议另一个单元测试框架使用当前版本的dotnet核心?

Appreciate any help that you can provide. Thank you!

感谢您提供的任何帮助。谢谢你!

3 个解决方案

#1


26  

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

更新4:NUnit3TestAdapter v3.8已经发布,所以它不再是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:

更新3:使用NUnit3TestAdapter v3.8.0-alpha1,现在可以使用dotnet测试命令运行测试。您只需要在您的测试项目中拥有这些依赖项:

<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.

更新2:Visual Studio 2017和迁出项目。json到csproj使dotnet-test-nunit测试适配器过时,因此我们需要发布另一个更新的适配器来运行。net核心测试。如果您正在使用VS2017和新的。net核心工具,请参见在Visual Studio 2017中使用NUnit测试。net Core。如果您正在使用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现在支持dotnet测试,所以您不再需要使用NUnitLite。参见测试。net Core RC2和ASP。使用NUnit 3的净核RC2。


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.

NUnit控制台(以及底层的NUnit引擎)还不支持对。net core进行单元测试。希望我们能在NUnit 3.4中得到支持。

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

同时,您可以使用NUnitLite将您的测试切换到一个自动执行的测试运行器。

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

我在使用NUnit 3测试。net Core的过程中写了一篇博文。一个快速的总结;

  1. Create a .NET Core Console application for your test project.
  2. 为您的测试项目创建。net Core控制台应用程序。
  3. Reference NUnit and NUnitLite from your test project. You do not need the runner.
  4. 从测试项目中引用NUnit和NUnitLite。你不需要跑步者。
  5. Modify main() to execute the unit tests.
  6. 修改main()以执行单元测试。

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.

有关更完整的信息,请参阅我的博客文章。

#2


3  

Did as @Rob-Prouse suggested with minor changes. It finally works now.

按照@Rob-Prouse的建议做了一些小小的改动。现在最后的作品。

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);

    }
}

#3


0  

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.

注意,您可能还需要将项目从类库转换为控制台应用程序。

#1


26  

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

更新4:NUnit3TestAdapter v3.8已经发布,所以它不再是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:

更新3:使用NUnit3TestAdapter v3.8.0-alpha1,现在可以使用dotnet测试命令运行测试。您只需要在您的测试项目中拥有这些依赖项:

<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.

更新2:Visual Studio 2017和迁出项目。json到csproj使dotnet-test-nunit测试适配器过时,因此我们需要发布另一个更新的适配器来运行。net核心测试。如果您正在使用VS2017和新的。net核心工具,请参见在Visual Studio 2017中使用NUnit测试。net Core。如果您正在使用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现在支持dotnet测试,所以您不再需要使用NUnitLite。参见测试。net Core RC2和ASP。使用NUnit 3的净核RC2。


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.

NUnit控制台(以及底层的NUnit引擎)还不支持对。net core进行单元测试。希望我们能在NUnit 3.4中得到支持。

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

同时,您可以使用NUnitLite将您的测试切换到一个自动执行的测试运行器。

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

我在使用NUnit 3测试。net Core的过程中写了一篇博文。一个快速的总结;

  1. Create a .NET Core Console application for your test project.
  2. 为您的测试项目创建。net Core控制台应用程序。
  3. Reference NUnit and NUnitLite from your test project. You do not need the runner.
  4. 从测试项目中引用NUnit和NUnitLite。你不需要跑步者。
  5. Modify main() to execute the unit tests.
  6. 修改main()以执行单元测试。

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.

有关更完整的信息,请参阅我的博客文章。

#2


3  

Did as @Rob-Prouse suggested with minor changes. It finally works now.

按照@Rob-Prouse的建议做了一些小小的改动。现在最后的作品。

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);

    }
}

#3


0  

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.

注意,您可能还需要将项目从类库转换为控制台应用程序。