Dealing with NUnit test fixtures that don’t get on

When we want to test the behaviour for a particular object is working correctly we setup a unit test method and mock it’s dependencies such as it’s data access that way we can setup expectations and run the tests without requiring items such as a running database or an available web service, but eventually if we want to cover ourselves we are going to need to test that the data access code talks to the database correctly. If we aren’t careful these tests could slow down the running of the non data access tests this can lead to developers neglecting to run tests if they start taking longer and longer to run one way to deal with these troublesome unit tests in NUnit is to supply a Category attribute on the fixture class

[Category("data access tests")]
public class MyTestFixture
{
//...snip
}

Then by using the Categories tab you can add the new category to the selected categories list and then choose to exclude these categories, When you switch back to the Tests tab the tests in this category are greyed out and can be ignored, then when you come to do some work on data access code you can include the category and check you haven’t broken anything!

Advertisements