Sorted By: Tag (xunit)

Unit Testing With xUnit.net for ASP.NET MVC Web Applications - Part 1

In this blog post, we will see how we set up our environment for xUnit.net Unit Testing Framework. This is the first blog post of the blog post series on Unit Testing With xUnit.net for ASP.NET MVC.
2011-10-16 14:40
Tugberk Ugurlu


you-need-some-tests-yo...

I have been developing web applications with ASP.NET MVC for long time and to be honest, none of them has good Unit Testing backing them up. I don’t know you have realized but I am not even mentioning anything about TDD (Test Driven Development).

This isn’t a big problem for me. It is mainly because I work in a team, which has only one member inside it [yeah, it’s me :)], for my company. But all the cool guys are always talking about how awesome it is to work with Unit Testing.

This made me obsessed about Unit Testing and I stated to spend real quality time on investigating that so that I will be able to sleep without having dreams about a guy who wears an ugly sunglasses and telling me ‘You need some tests yo!’. (No, I am just kidding, I am not insane. At least, not yet.)

Visual Studio has a built-in test environment but I figured that is not enough. So, from now on, I will be using xUnit.net for my unit test applications.

What is xUnit.net?

xUnit.net is a unit testing tool for the .NET Framework. Written by the original inventor of NUnit, xUnit.net is the latest technology for unit testing C#, F#, VB.NET and other .NET languages. Works with ReSharper, CodeRush, and TestDriven.NET.

Set Up Your Environment

First thing is first. Go to http://xunit.codeplex.com/releases/view/62840 to download the latest version of xUnit.net. At the time that I write this blog post, the latest version of xUnit.net is 1.8. Unzip the file inside a path and run the xUnit.installer.exe file.

You will have a window opened in front of you. For the sake of simplicity, I only check Microsoft ASP.NET MVC 3 Unit Testing Templates and click Apply button. After that, here is the screen shots of the result :

imageimage

File > New > Project

After the installation, go to VS 2010 and follow File > New > Project > ASP.NET MVC 3 Web Application. You will see that xUnit is sitting right there.

image

imageGo ahead and create the project. When you create the project you will see the usual HomeController.cs inside your Controllers folder. With xUnit.net project, you will see that we get HomeControllerFacts.cs file out of the box as sample. This file gives us a head start on this new thing for us.

On this blog post, I would like to focus on how things goes for us instead of how we write our unit test code.

As we have seen, we get a sample project and sample test project with some tests written inside it the the sample application. When we build the solution, we should be able to process successfully.

So, the next question is how we run our tests. The xUnit.net framework supports 4 distinct test runners:

  • GUI Test Runner
  • Console Test Runner
  • TestDriven.NET Test Runner
  • Resharper 4.0 Test Runner

I will walk you through how we run our tests with GUI Test Runner for this blog post.

Running xUnit Tests With GUI Test Runner

Navigate to the path which you have installed you xUnit project files and you will notice 4 files there which are green as follows :

image

Firstly, they are not same. I am sure you already understood what is for what but here is the detailed info :

xuit.gui.clr4, xuit.gui.clr4.x86 : This is for CLR 4 projects. .x86 is for x86 compatibility which you already know.

xuit.gui, xuit.gui.x86 : This is for CLR 2 projects. .x86 is for x86 compatibility which you already know.

I have open up the xuit.gui.exe which is suitable for me. Then, from the GUI, select the menu option Assembly > Open (Ctrl + O for keyboard shortcut) and select your test application output dll file (Be careful here. If you don’t build your solution before this process, you won’t be able to find the output dll file).

image

When you make the selection, the runner will be able to pick up all the tests as you can see below :

image

Let’s touch the Run All button on the left side at the bottom :

image

Looks cool. I break one of the tests on purpose so that we can see it failing :

image

So, this is the simplest as it can get. This blog post is supposed to be the first blog post of the blog post series on Unit Testing With xUnit.net for ASP.NET MVC and I will be blogging on further features of xUnit for ASP.NET MVC. Stay tuned!

Tags