## Excluding integration tests in NUnit's GUI, part 1 - Using categories

| tags:

After writing unit tests for my personal projects for a while, now is the first time that I’m writing integration tests.
So far, none of the projects I wrote tests for did involve any external components (or at least none that couldn’t be mocked).

But my current project needed a database, and I decided to use RavenDB to get familiar with NoSQL.
According to Ayende, the recommended way to test RavenDB apps is directly using the database in in-memory mode. So here I am now, writing integration tests.

After running the first test for the first time, I was reminded that integration tests are slow - the in-memory database takes a few seconds to initialize on each run.
I don’t want to wait each time I run my tests, so I went to Google and the NUnit docs to find out how to make NUnit ignore the integration tests when I don’t want to run them (the NUnit version I’m using here is 2.6.2, by the way).

The first thing I found is the [Category] attribute, which does almost what I wanted.

To use it, I just need to mark the test class in question with the attribute:

namespace Tasko.Tests
{
[TestFixture]
[Category("Integration")]
public class RavenEmbeddedTests
{
// tests here
}
}


Now I can tell the GUI to only run tests with a certain attribute, or to run all tests except those with a certain attribute.

The documentation says how to do this:

This feature is accessible by use of the /include and /exclude arguments to the console runner and through a separate “Categories” tab in the gui. The gui provides a visual indication of which categories are selected at any time.

This sounds like the only way to do this in the GUI is to navigate to the “Categories” tab, add the category to the list below and tell NUnit to exclude the categories in the list…all by clicking manually with the mouse:

The problem is that I’m too lazy to do this by hand. All my projects with unit tests have a batch file to open the NUnit GUI, which looks like this:

path\to\nunit-x86.exe path\to\Tests.dll


Similar to this, I’d like to have two batch files to open the GUI…one to run all tests, and one to exclude the integration tests.

I didn’t find this in the documentation about the GUI’s command line options, but it turns out that the GUI understands /include and /exclude as well.

So I added a second batch file to run everything except the integration tests:

path\to\nunit-x86.exe path\to\Tests.dll /exclude:Integration


This makes the single integration test that I have at the moment appear with a different text color:

And now comes the part where I said at the beginning that it almost works for me:
In the Nunit GUI, there are two ways to run the tests - you can either click on the “Run” button or press F5.

Unfortunately, the results are different:

The “Run” button runs all tests except the “Integration” category, as expected:

But F5 runs all tests…including the “Integration” category:

Depending on your habits, this may be okay for you. After all, you can just use the button instead of F5 and everything works.

But I am so used to pressing F5…I literally never used the button at all.
So I kept on searching for a better solution, and I actually found one that works better for me.
More about that in the next post.