A famous quote “Prevention is better than cure” not only applies to the healthcare world, but in fact, it applies everywhere, even while developing software.
What I mean from above is, while developing software knowing scenarios & bugs in early stage (while coding) is way better than knowing them later in SDLC (Software development life cycle). Now a days Test driven development (TDD) and Test First approach is the modern way of developing high quality software in agile environment. TDD and TF allows programmers to think about all possible scenarios before and while writing code. These cases can work as granular functional spec. It not only ensures quality but also give developers a sense of ownership of functionality they are building.
There are several tools and frameworks available to incorporate TDD in development process. This blog is dedicated towards xUnit.Net tool.
xUnit is a unit testing tool for the .NET framework. It is created by nUnit creators. It is built to address failures and shortcomings observed in nUnit. And, it incorporates success observed in nUnit. xUnit demonstrates great ability to fit closely with .NET platform. It is free, open source and licensed under Apache (Version 2.0).
There are several features that distinguish xUnit from other unit testing tools like nUnit and MS Test. Following are the main features I would like to cover:
- Single object instance per test method: It allows complete isolation of test methods that allow developers to independently run tests in any order.
- No Setup & Teardown attributes: In nUnit you can create methods to run before and after an actual test method runs. These methods are decorated with attributes named SetUp and TearDown respectively. The down side is that it unnecessarily creates confusion and make developers to hunt around the presence and absence of such methods. It is more productive to write code set up and tear down code within test methods itself. Hence, in xUnit.Net pink slip is given to these unproductive attributes.
- No Support for ExpectedException attribute: This is another attribute to whom xUnit has said “Good bye”. The reason is expected exceptions could be thrown from wrong place in code that could potentially pass the test while it was supposed to be failed. The best way is that the developers handle these expected exceptions within the test method itself. This approach provides better control over writing arrange, act, assert and handling exceptions.
- Reduced set of attributes: To make the framework simple in use xUnit has less number of attributes as compared with other tools. For example, unlike nUnit it doesn’t require [TextFixture] & [TestMethod] attributes to declare a test while it requires only [Fact] attribute decorated on test method.
- Automation: xUnit has great ability for test automation and can smoothly work in conjunction with other testing framework. It is highly extensible and open for customization.
In addition to the above features and differences with other tools, click here to get full comparison between xUnit vs nUnit vs Ms Test.
Simple Demo: The tests can run in several ways using:
- Visual studio 2013 test explorer/runner
- Resharper (#R)
- Commad line
- TestDriven.NET and so on
I created a simple demo to demonstrate the following topics:
- Installation of xUnit
- Creation of test using simple [Fact] attribute and Assert
- Nuget packages and extensions involved
- Run test using visual studio test explorer/runner and Resharper.
Lastly, Quality matters. Mind It.