问题
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:
- I attached another web app project, dedicated to writing tests, to my original project, as opposed to the console library project.
- Then I downloaded NuGet packages for : "dotnet-test-nunit", "NUnit", "NUnit.Console" and "NUnit.Runners".
- 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:
- It don't seems like an optimized process - how can I optimize this?
- 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,
- Add a .NET Core Class Library project to your solution
- Add the
dotnet-test-nunit
andNUnit
NuGet packages to the project - Reference your web app from your test project
- Write some tests
- 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