我正在尝试使用.NET Core为我的C#项目运行单元测试。我在运行时使用Docker容器。
Dockerfile
FROM microsoft/dotnet:0.0.1-alpha
RUN mkdir /src
WORKDIR /src
ADD . /src
RUN dotnet restore
“ NUnit”和“ NUnit.Runners”已添加到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
...
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.
我尝试用以下方式运行测试:
dotnet nunit
dotnet nunit-console
但是它不起作用。
我该如何致电跑步者?还是有另一个可以与当前版本的.NET Core配合使用的单元测试框架?
更新4: NUnit3TestAdapter v3.8已发布,因此不再是alpha。
更新3:使用NUnit3TestAdapter v3.8.0-alpha1,现在可以使用dotnet test
命令运行测试。您只需要在测试项目中具有这些依赖项:
<PackageReference Include="nunit" Version="3.7.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.8.0-*" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.*" />
您可以尝试!
Update 2: Visual Studio 2017和从project.json
到csproj
的迁移使dotnet-test-nunit
测试适配器过时,因此我们需要发布另一个更新的适配器来运行.NET Core测试。如果您使用的是VS2017和新的.NET Core工具,请参阅Testing .NET Core with NUnit in Visual Studio 2017。如果使用的是project.json
,请参见下面的更新。
更新: NUnit现在支持dotnet test
,因此您不再需要使用NUnitLite。参见testing .NET Core RC2 and ASP.NET Core RC2 using NUnit 3。
NUnit控制台(和底层的NUnit Engine)尚不支持针对.NET Core运行单元测试。希望我们将在NUnit 3.4中获得该支持。
同时,您可以使用NUnitLite将测试切换到自动执行的测试运行器。
我在Testing .NET Core using NUnit 3上写了一篇有关该过程的博客文章。快速摘要是;
应该看起来像这样;
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);
}
}
}
有关更多完整信息,请参见my blog post。
我做为Rob-Prouse suggested,只是做了些微改动。现在终于可以使用了。
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);
}
}
对答案进行一些调整,使其编译。
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);
}
}
}
}
请注意,您可能还必须将项目从类库转换为控制台应用程序。