0

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 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer. Your blog really helped while displaying step by step process.
I must thank you as well Rob, my tests are running in Visual Studio fine. I cannot seem to get them to run on VSTS / Visual Studio Online though. All the articles I have found suggest working with test adapter nuget packages - none of which are supported under .net core.
I tried full implementation for local and VSTS use of NUnit, MSTest and xUnit and found xUnit the best supported - details and links at stackoverflow.com/q/40093573/2579219

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.