在dotnet核心中运行NUnit测试
我正在尝试使用dotnet core为我的c#项目运行unit testing。 我正在使用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
不行。
所以我的问题是如何打电话给跑步者? 或者有人可以建议另一个与当前版本的dotnet核心一起使用的unit testing框架吗?
感谢您提供的任何帮助。 谢谢!
更新4: NUnit3TestAdapter v3.8已经发布,因此不再是alpha版。
更新3:使用NUnit3TestAdapter v3.8.0-alpha1 ,现在可以使用dotnet test
命令运行测试。 您只需要在测试项目中拥有这些依赖项:
你可以尝试一下!
更新2: Visual Studio 2017和从project.json
迁移到csproj
使得dotnet-test-nunit
测试适配器过时,因此我们需要发布另一个更新的适配器来运行.NET Core测试。 如果您使用的是VS2017和新的.NET Core工具,请参阅在Visual Studio 2017中使用NUnit测试.NET Core 。 如果您使用的是project.json
请参阅下面的更新。
更新: NUnit现在支持dotnet test
,因此您不再需要使用NUnitLite。 请参阅使用NUnit 3测试.NET Core RC2和ASP.NET Core RC2 。
NUnit控制台(和底层NUnit引擎)不支持对.NET核心运行unit testing。 希望我们能在NUnit 3.4中获得支持。
在此期间,您可以使用NUnitLite将测试切换为自动执行的测试运行器。
我在使用NUnit 3测试.NET Core的过程中写了一篇博文。 快速总结是;
- 为您的测试项目创建.NET Core Console应用程序。
- 从测试项目中引用NUnit和NUnitLite 。 你不需要跑步者。
- 修改
main()
以执行unit testing。
它看起来应该是这样的;
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); } } }
有关更完整的信息,请参阅我的博客文章 。
正如@ 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); } }
一些调整答案,使其编译。
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); } } } }
请注意,您可能还必须将项目从类库转换为控制台应用程序。