Quantcast
Channel: Category Name
Viewing all articles
Browse latest Browse all 10804

Unit testing with DNX (ASP.NET 5) projects

$
0
0

One common question that I get is “How do I unit test my ASP.NET 5 (DNX) projects?” The biggest reason for the question is because we don’t have a good starting point (File->New Project) for unit testing. I’m happy to say that today we’ve solved this problem. We now have templates for both Visual Studio and command line users. I’ll explain how you can get, and use, the templates here.

Note: this blog post is based on ASP.NET 5 beta6, if you don’t already have that installed please install it. More info at http://blogs.msdn.com/b/webdev/archive/2015/07/27/announcing-availability-of-asp-net-5-beta-6.aspx.

Adding an xUnit project using SideWaffle

If you are using Visual Studio and want to create a DNX unit test project, to get started first download a fresh copy of SideWaffle (version 1.19 or higher for xUnit). After you install SideWaffle you’ll have a new xUnit project template. Let’s walk through it. To get started open your existing solution using Visual Studio 2015 that has your DNX projects. From there go to File->New Project and navigate to the web node. From there you should see the following list of available templates.

image

In the image above you’ll see a new xUnit test project. Use that to create your test project. When you use that template here is what you get in Visual Studio.

image

The content for Class1.cs and project.json are shown below.

project.json

{
  "version": "1.0.0-*",
  "description": "UnitTestProject1 test project",
  "authors": [ "sayedha" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",

  "dependencies": {
    "xunit": "2.1.0-beta4-build3109",
    "xunit.runner.dnx": "2.1.0-beta4-build134"
  },
  "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"
      }
    }
  }
}

Class1.cs

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

namespace UnitTestProject1
{
    // 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 Class1
    {

        [Fact]
        public void UniTest1()
        {
            Assert.True(true);
        }
    }
}

At this point in Visual Studio you can build your solution and then open Test Explorer to see the test cases as shown in the screenshot below.

image

That’s it, from here you should be able to create, run and debug xUnit test cases in Visual Studio 2015. Now let’s move on for the command line users.

Adding an xUnit project using yeoman for command line users

If you are not using Visual Studio that’s OK, we’ve got you covered. We have previously blogged about the yo aspnet yeoman generator, see these posts for more details.

You can get started with yo aspnet pretty easily, see the instructions in the yo aspnet readme. After you have yo aspnet installed the latest version of yo aspnet when you invoke yo aspnet form the command line you should see the following.

image

In the screenshot above (running on my Mac) you can see that we now have a Unit test project. Select that project type, hit enter and give your project a new name. After that a new project is added with the same contents as above. From there you can cd into the directory and run dnu restore to restore the NuGet packages and then dnx . test to run all the test cases. See the screenshot below which shows this.

image

In the screenshot above you can see that one test case was executed and that it passed.

 

That is pretty much all that you need to get started with creating xUnit test cases for DNX projects. In the future we are hoping to add unit testing templates into Visual Studio itself (i.e. w/o SideWaffle). Please let me know if you try it in the comments below.

 

Sayed Ibrahim Hashimi – @SayedIHashimi


Viewing all articles
Browse latest Browse all 10804

Trending Articles