使用ASP.NET MVC Core 1.0配置Nunit [英] Configuring Nunit with ASP.NET MVC Core 1.0

查看:103
本文介绍了使用ASP.NET MVC Core 1.0配置Nunit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的ASP.NET Core 1.0项目配置NUnit.我尝试了以下操作: http://www .alteridem.net/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 : http://www.alteridem.net/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.

这对我有用:

  1. 与控制台库项目相反,我将另一个专门用于编写测试的Web应用程序项目附加到了我的原始项目中.
  2. 然后,我为以下站点下载了NuGet软件包:"dotnet-test-nunit","NUnit","NUnit.Console"和"NUnit.Runners".
  3. 然后我编写了一个测试,该测试在将以下行添加到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%" ]
  }
}`

我有以下问题:

  1. 这似乎不是一个优化的过程-我该如何优化呢?
  2. 为什么测试不能与控制台应用程序一起使用,可以做些什么来使它起作用?

任何帮助将不胜感激.我对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.RunnersNUnit.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,

  1. 将.NET Core类库项目添加到您的解决方案中
  2. dotnet-test-nunitNUnit NuGet软件包添加到项目中
  3. 从测试项目中引用您的Web应用
  4. 写一些测试
  5. 从Visual Studio运行测试
  1. Add a .NET Core Class Library project to your solution
  2. Add the dotnet-test-nunit and NUnit NuGet packages to the project
  3. Reference your web app from your test project
  4. Write some tests
  5. 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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