Unit testing in Asp .Net 5(vNext) and mvc 6 using XUnit

In this post we will see how to create Unit tests for Asp. Net 5 (vNext) projects.

We will go step by step.

First of all Let us see what is xUnit?

xUnit.net is a free, open source, community-focused unit testing tool for the .NET Framework. Written by the original inventor of NUnit v2, xUnit.net is the latest technology for unit testing C#, F#, VB.NET and other .NET languages.

You can find more about it here.

You might be thinking why we can not do just “Add New Project -> Unit test project” for vNext?
No not yet but yes we can!

Confused?
Well there were not any templates supported to create Unit test in vNext until we had newly added templates in SideWaffle.

Now the question arrises like “What is SideWaffle?”
SideWaffle is an extension

The SideWaffle extension adds a bunch of useful Snippets, Project- and Item Templates to Visual Studio. The purpose is to make your daily work in Visual Studio a richer and more productive experience.

Got it, How to get SideWaffle?
You can directly install it from here.

Now you can directly select template for Unit tests.

How?
File->New Project and navigate to the web node. From there you should see the following list of available templates.

2248.image_thumb_0C9B3FA8

There you go! You have just created an Unit test project which would look as below:

0636.image_thumb_5B77ADEE

Anything in project.json?

{
"version": "1.0.0-*",
"description": "FirstDemoTests Class Library",
"authors": [ "Neel" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",

"dependencies": {
"System.Collections": "4.0.10-beta-23109",
"System.Linq": "4.0.0-beta-23109",
"System.Threading": "4.0.10-beta-23109",
"System.Runtime": "4.0.10-beta-23109",
"Microsoft.CSharp": "4.0.0-beta-23109",

"xunit.runner.dnx": "2.1.0-*",
"xunit": "2.1.0-*",

"YetAnotherWebbyDemo": "1.0.0-*"
},

"commands": {
"test": "xunit.runner.dnx"
},

"frameworks": {
"dnx451": {},
"dnxcore50" : {
      "dependencies": {
        "Microsoft.CSharp": "4.0.0-beta-22816",
        "System.Collections": "4.0.10-beta-22816",
        "System.Linq": "4.0.0-beta-22816",
        "System.Threading": "4.0.10-beta-22816"
      }
    }
 }
}

Now you are all set to write your first method of Unit test in vNext.

Suppose you have method in your AccountController class which validates that User is authorized or not.

So, in your AccountControllerTest class we would write something as below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace FirstDemoTests
 {
 // This project can output the Class library as a NuGet Package.
 // To enable this option, right-click on the project and select the Properties menu item. In the Build tab select "Produce outputs on build".
 public class AccountControllerTests
 {
    [Fact]
    public void IsAuthorizedTest()
    {
       var accountController = new AccountController();
       Assert.True(accountController.IsAuthorized());
    }
 }
 }

You can check your results in Visual Studio. You can build your solution and then open Test Explorer to see the test cases.

That’s it! You can add Unit tests in vNext.

Stay tuned for more updates.

Advertisement

2 thoughts on “Unit testing in Asp .Net 5(vNext) and mvc 6 using XUnit

    1. Hi Juanperez, I guess you mean code coverage! According to my knowledge currently there is “OpenCover” for generating code coverage, “Coveralls” for publishing the code coverage publicly, and “ReportGenerator” for generating local code coverage reports.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s