Unit / Integration testing in ASP.NET 5 and Visual Studio 2015 using XUnit.Net



Currently in Visual Studio 2015 CTP 6 MSTests are not supported to test ASP.NET vNext projects.

If you create MSTest project you are unable to add a reference to the ASP.NET vNext project.

Microsoft will enable support for MSTests for ASP.NET vNext in the next release of Visual Studio.

Until then you can only write unit and integration tests using XUnit.

First, you will need to create .NET core class library project where you will have all your tests:

ASP.NET5ClassLibrary

Reference vNext project or any other .NET project (you can reference regular .NET projects from vNext Class Library) you want to test.

Create your test class and test method similar to this:

public class TestClass
{ 
 [Fact]
 public void PassingTest()
 {
     Assert.Equal(3, 2);
 }
}

 

Add reference to these 3 NuGet packages:

"xunit": "2.1.0.0-beta1-build2945",
"xunit.runner.aspnet": "2.1.0.0-beta1-build60",
"xunit.runner.visualstudio": "2.1.0.0-beta1-build1051"

 

Make sure Test Explorer is visible ( Test > Windows > Test Explorer).

Every time you build your project, XUnit.Net will discover unit tests in your project.

Now you can run or debug your unit or integration tests.

If you like this article don’t forget to subscribe to this blog and make sure you don’t miss new upcoming blog posts.

 

  • Daniel Zolnjan

    Have you managed to get test explorer working (showing tests) with beta6 and vs2015RTM?

    • I am currently in process of migration project to beta6 and still have some issues with that.
      After that I will try to enable unit tests support.
      If you manage to figure out how to do this before me please try to publish some blog post about that and drop the link here.
      Thanks Daniel.