Configuring Nunit with ASP.NET MVC Core 1.0

青春壹個敷衍的年華 提交于 2019-12-13 08:24:38

问题


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.

Here is what worked for me:

  1. I attached another web app project, dedicated to writing tests, to my original project, as opposed to the console library project.
  2. Then I downloaded NuGet packages for : "dotnet-test-nunit", "NUnit", "NUnit.Console" and "NUnit.Runners".
  3. Then I wrote a test which worked after adding this line to project.json: "testRunner": "nunit"

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%" ]
  }
}`

I have following questions:

  1. It don't seems like an optimized process - how can I optimize this?
  2. Why test didn't worked with console application, could something be done to make that work?

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.

Thanks.


回答1:


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.

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. 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

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

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.



来源:https://stackoverflow.com/questions/39763452/configuring-nunit-with-asp-net-mvc-core-1-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!