在.NET Core中运行NUnit测试 [英] Run NUnit tests in .NET Core

查看:60
本文介绍了在.NET Core中运行NUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用.NET Core为我的C#项目运行单元测试.我在运行时使用 Docker 容器.

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

Dockerfile

Dockerfile

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

"NUnit"和"NUnit.Runners"已添加到project.json

"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": { }
}

使用以下输出成功运行 dotnet restore

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

但这是行不通的.

我该怎么称呼跑步者?还是有另一个可以与当前版本的.NET Core一起使用的单元测试框架?

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

推荐答案

更新4:

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

更新3:使用 NUnit3TestAdapter v3.8.0-alpha1 ,现在可以运行测试了使用 dotnet test 命令.您只需要在测试项目中具有以下依赖项即可:

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.*" />

您可以尝试一下!

更新2:,Visual Studio 2017和从 project.json csproj 的迁移使 dotnet-test-nunit 测试适配器已过时,因此我们需要发布另一个更新的适配器以运行.NET Core测试.请参阅在Visual Studio 2017中使用NUnit测试.NET Core(如果您使用的是VS2017和新的.NET Core工具).如果您使用的是 project.json ,请参见下面的更新.

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.

更新:NUnit现在支持 dotnet测试,因此您不再需要使用NUnitLite.参见测试.NET Core RC2和ASP.NET使用NUnit 3的Core RC2 .

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控制台(和底层的NUnit Engine)尚不支持针对.NET Core运行单元测试.希望我们将在NUnit 3.4中获得该支持.

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.

同时,您可以使用NUnitLite将测试切换到自动执行的测试运行程序.

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

我在

  • 为您的测试项目创建.NET Core控制台应用程序.
  • 参考 NUnit
    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.

    这篇关于在.NET Core中运行NUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆