Unit Testing with xUnit and ReSharper

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:

  • /ModelTests/
    (contains all the tests for the Model Project)
  • /WebTests/
    (contains all the tests for the Web Project)
  • /TestRespository/
    (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:

  • /ModelTests/FoobarTests.cs

Creating a Simple Test

Using the Foobar class as an example:

namespace SolutionName.Model
    public class Foo
        public int MyValue { get; set;}

        public Foo(int initVal)
            MyValue = initVal;

        public int Bar(int toAdd)
            if (toAdd < 10)
                MyValue += toAdd ;

            return toAdd;

I wrote the following (far from exhausitve) example test:

namespace SolutionName.Tests.xUnit.ModelTests
    public class FooTests
        private Foo thisFoo;

        public FooTests()
            thisFoo = new Foo(5);

            thisFoo = null;

        public void TestFoo_MyValueHasExpectedValue_AfterInstanciation()
            Assert.Equal(5, thisFoo.MyValue);

        public void TestFooBar_MyValueIsSame_AfterBarMethodPassingValueGreaterThanTen()

            int expectedValue = thisFoo.MyValue;//[ARRANGE]

            Assert.Equal(expectedValue, thisFoo.MyValue);//[ASSERT]

        public void TestFooBar_MyValueExpectedValue_AfterBarMethodPassingValueLessThanTen()
            Assert.Equal(5, thisFoo.MyValue);

            int newValue = 9;
            int expectedValue = thisFoo.MyValue + newValue;


            Assert.Equal(expectedValue, thisFoo.MyValue);

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:

  1. Select the Resharper Menu > Plugins…
  2. 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.

Running unit tests in ReSharper
Running unit tests in ReSharper

To run your tests simply click the icon for your test class and choose Run or Debug to debug your code.

Lewis Barclay
Web Development and Web Consultancy


One thought on “Unit Testing with xUnit and ReSharper

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 )

Google+ photo

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

Connecting to %s