4

I currently have a Asp.net Core Web.Api project.
It (only) targets the net452 framework.
Question is: How do I unit test an asp,net core WebAPI (net452) project?

I already tried adding a classic .net unit testing project to my solution, with a reference to my web.api core project. For some reason, I'm not able to declare (refer) any class inside my webapi project. From this, I conclude, I cannot use a classic unit testing project to test my Asp.net core project.

inside my project.json:

 "frameworks": {
   "net452": {}
   }
}

this is how my solution looks like:

solution

inside my test class:

[TestClass]
public class ActiveDirectoryClientProviderTests
{
    [TestMethod]
    public async Task Get_Should_Return_Client()
    {
        //var settings = new ActiveDirectorySettings();
        Ng2Aa_demo.Domain.Avatar.GetAvatarByUserHandler
    }
}

2 Answers 2

3

You can use xUnit or NUnit, both works well with .Net core.

Here is a tutorial on how to create the unit test project: https://learn.microsoft.com/en-us/dotnet/articles/core/testing/unit-testing-with-dotnet-test

You basically create a .Net core class library, import the xUnit or NUnit library and set the unit test runner.

An example of a project json using xUnit:

{
  "version": "1.0.0-*",
  "testRunner": "xunit",

  "dependencies": {
    "Microsoft.NETCore.App": {
      "type":"platform",
      "version": "1.0.0"
    },
    "xunit":"2.1.0",
    "dotnet-test-xunit": "1.0.0-rc2-192208-24",
    "PrimeService": {
      "target": "project"
    }
  },
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      },
      "imports": [
        "dotnet5.4",
        "portable-net451+win8"
      ]
    }
  }
}

Taken from here

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

Comments

0
  1. First you need to add another core project enter image description here

  2. Inside your project.json add 2 dependencies & specify the testRunner. Make sure to use Xunit !!

enter image description here

"xunit": "2.2.0-beta2-build3300",
"dotnet-test-xunit": "2.2.0-preview2-build1029"

Now you can unit test your web api core project with xunit !

Comments

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.