Monday, 29 February 2016

Unit Test for ASP.Net 5 with xUnit

Unit testing plays a significant role in assuring the quality of the applications we develop. To unit test an ASP.Net 5 RC1 Update1 (ASP.Net Core 1.0 now), web application we can use xUnit.
To add xUnit test to the solution add a Class Library (Package) project.image
image
Edit the default project.json shown below.image
Add dependency to the ASP.Net 5 web project, and to xUnit and xUnit.Runner.Final project.json should be similar to below.image

{
  "version": "1.0.0-*",
  "description": "BookMyEvents.UnitTests Class Library",
  "authors": [ "Chaminda" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",

  "dependencies": {
    "BookMyEvents": "1.0.0-*",
    "xunit": "2.1.0",
    "xunit.runner.dnx": "2.1.0-rc1-build204"
  },

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

  "frameworks": {
    "dnx451": { },
    "dnxcore50": {}
  }
}
Once the project.json saved the references get updated.image
Let’s write a test for a very simple controller method.image
Test method as a Fact (There are two types of Unit Tests in xUnit. Fact and Theory. More information here).image
Go to test explorer in VS to view the test.image
image
If you cannot see test as above build your solution. Test will be then available in the explorer. Execute and you can see the results.image
Next post let’s learn how to run the unit tests (xUnit), with VS Team Service build and publish test results.

2 comments:

Hani VJ said...

Good info! thanks for the share Useful in web and asp .net development services Here is a good resource if anyone in need of mobile application development services  asp .net developmers chennai

Software Development Company said...

Hello,
The Article on Unit Test for ASP.Net 5 with xUnit is very informative. It give detail information about it .Thanks for Sharing the information on Unit testing for ASP.net.
Software Testing Services