In this Post:
I’ve just started using the xUnit framework, and inherently the xUnit approach as a unit testing framework within .Net. For the purpose of this post and to avoid confusion when I say xUnit, I’m talking specifically about the framework!
I’m using ReSharper and the integration is really useful. I have a VS2008 standard edition license and it adds the UI point and click testing environment I lack, not having the heavier weight VS editions.
I’m currently building an MVC application and have abstracted out the model into seperate project(s).
This leaves me with the following projects as part of my solution:
- NewSolutionName.Model [C# classes : Business Logic]
- NewSolutionName.Web [MVC / ASP.Net (C#) : UI]
- NewSolutionName.Tests.xUnit [C# Classes : Unit tests]
Setting up the xUnit unit test project
Add a reference to xunit.dll so that you can write unit tests using xUnit , as well as referencing any of the projects that you are going to write unit tests for, in my case both the Model and Web Projects.
I structured my unit test project into the followiung folders:
(contains all the tests for the Model Project)
(contains all the tests for the Web Project)
(contains all Mock / Dummy / Stub etc and helper class code)
I collate my tests into logical collections. So unit tests for a Foobar class, in my model layer, would be created in a class in the /ModelTests folder:
Creating a Simple Test
Using the Foobar class as an example:
I wrote the following (far from exhausitve) example test:
Developing units with xUnit sits more naturally than other frameworks I’ve used. I try to think of the test itself as a self contained object that has a constructor for setup and destructor for tear down (as well as helper methods where necessary) and then each method should contain the following:
- ARRANGE – this should be any code that needs to setup the test Mock / Dummy objects etc.
- ACT – Execute the actual functionality being tested
- ASSERT – check that the results of the test are as expected
The test we’re interested above is:
Where I marked the AAA code!
Running the test From ReSharper
The first thing to do is to check that the xUnit Test Runner plugin is installed and recognised by ReSharper:
- Select the Resharper Menu > Plugins…
- There should be a plugin entitled : xUnit.Net Runner for ReSharper 4.0
If so you should see green and yellow ‘test icons’ on the left hand side of your code, one for your test class (to run all tests) and then one for each individual test.
To run your tests simply click the icon for your test class and choose Run or Debug to debug your code.
Web Development and Web Consultancy