使用ASP.NET MVC Core 1.0配置Nunit

编程入门 行业动态 更新时间:2024-10-26 13:27:22
本文介绍了使用ASP.NET MVC Core 1.0配置Nunit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想为我的ASP.NET Core 1.0项目配置NUnit.我尝试了以下操作: www .alteridem/2015/11/04/testing-net-core-using-nunit-3/ ,但适用于控制台应用程序.

I wanted to configure NUnit for my ASP.NET Core 1.0 project. I have tried following : www.alteridem/2015/11/04/testing-net-core-using-nunit-3/ , but it's there for console applications.

如果我尝试将新项目(类库)附加到我的解决方案中,然后尝试引用我的原始项目-它给了我参考错误.

If I try to attach a new project (class library) to my solution and then try to reference my original project - it gives me reference error.

这对我有用:

  • 与控制台库项目相反,我将另一个专门用于编写测试的Web应用程序项目附加到了我的原始项目中.
  • 然后,我为以下站点下载了NuGet软件包:"dotnet-test-nunit","NUnit","NUnit.Console"和"NUnit.Runners".
  • 然后我编写了一个测试,该测试在将以下行添加到project.json后可以工作:"testRunner": "nunit"
  • 这是我的测试项目中的project.json文件:

    This is the project.json file from my test project:

    `

    { "dependencies": { "Microsoft.NETCore.App": { "version": "1.0.1", "type": "platform" }, "Microsoft.AspNetCore.Diagnostics": "1.0.0", "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", "Microsoft.AspNetCore.Server.Kestrel": "1.0.1", "Microsoft.Extensions.Logging.Console": "1.0.0", "JustForTest": "1.0.0-*", "NUnit": "3.4.1", "NUnit.Runners": "3.4.1", "NUnit.Console": "3.4.1", "dotnet-test-nunit": "3.4.0-beta-2" }, "tools": { "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final" }, "frameworks": { "netcoreapp1.0": { "imports": [ "dotnet5.6", "portable-net45+win8" ] } }, "buildOptions": { "emitEntryPoint": true, "preserveCompilationContext": true }, "runtimeOptions": { "configProperties": { "System.GC.Server": true } }, "publishOptions": { "include": [ "wwwroot", "web.config" ] }, "testRunner": "nunit", "scripts": { "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] } }`

    我有以下问题:

  • 这似乎不是一个优化的过程-我该如何优化呢?
  • 为什么测试不能与控制台应用程序一起使用,可以做些什么来使它起作用?
  • 任何帮助将不胜感激.我对TDD还是陌生的,因此在C#中进行编程-如果您在这种情况下有任何建议,也请分享一下.

    Any help will be much appreciated. I am new to TDD and for that matter programming in C# - if you have any suggestion in this context, kindly share that as well.

    谢谢.

    推荐答案

    您不需要NUnit.Runners或NUnit.Console NuGet软件包.您在我的博客上关注的帖子是使用NUnitLite的,这是早期使用NUnit编写.NET Core单元测试的唯一方法.从那时起,dotnet-test-nunit是必经之路.它与Visual Studio集成在一起,允许您使用dotnet test命令从测试资源管理器"窗口或命令行运行测试.

    You don't need the NUnit.Runners or NUnit.Console NuGet packages. The post on my blog that you are following is for using NUnitLite which was the only way to write .NET Core unit tests with NUnit early on. Since then, dotnet-test-nunit is the way to go. It integrates with Visual Studio allowing you to run your tests from the Test Explorer window or from the command line using the dotnet test command.

    您的单元测试项目应该只是.NET Core类库,而不是控制台应用程序或Web应用程序.高级步骤是

    Your unit test project should just be a .NET Core Class Library, not a console application or web app. The high level steps are,

  • 将.NET Core类库项目添加到您的解决方案中
  • 将dotnet-test-nunit和NUnit NuGet软件包添加到项目中
  • 从测试项目中引用您的Web应用
  • 写一些测试
  • 从Visual Studio运行测试
  • Add a .NET Core Class Library project to your solution
  • Add the dotnet-test-nunit and NUnit NuGet packages to the project
  • Reference your web app from your test project
  • Write some tests
  • Run your tests from Visual Studio
  • 有关更详细的演练,请阅读为.NET Core和ASP.NET Core编写NUnit 3测试.

    For a more detailed walkthrough, read my newer blog post on Writing NUnit 3 Tests for .NET Core and ASP.NET Core.

    您还应该查看用于集成测试的ASP.NET Core文档,它将提示您如何设置测试服务器,并确保您的Startup代码已执行.

    You should also look at the ASP.NET Core documentation for Integration Testing that will give you hints on how to setup a test server and make sure your Startup code gets executed.

    更多推荐

    使用ASP.NET MVC Core 1.0配置Nunit

    本文发布于:2023-11-05 07:23:02,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1560248.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:MVC   NET   ASP   Nunit   Core

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!