views:

22

answers:

1

I'm using NUnit 2.5.3, but if a more recent version of NUnit solves my problem, it won't be an issue to upgrade.

We have a couple of different categories for tests. Different categories of tests will be run during different flavours of build (fast running unit tests for checkins, all unit tests and other tests with external dependencies (database etc.) for overnighters etc.). Divorcing the true unit tests and the tests with external dependencies into different assemblies is a longer term objective, but I'll still want to enforce categories on the tests.

I want to ensure that all test methods have a single CategoryAttribute defined with a valid category name. A valid category name would be one from a list that I could define. However, just enforcing a Category on each test method would be a step in the right direction.

Does NUnit supports this kind of behaviour out of the box? Is there another NUnit attribute I can use other than CategoryAttribute which supports that kind of enforcement? Or is implementing a custom build step which checks the assemblies via reflection the way to go?

+1  A: 

I don't think there is a way to ensure that all tests are marked with the Category attribute - that's why I always split different types of tests into different assemblies (it shouldn't be too hard to split a legacy assembly, especially if you are using tools like Resharper). So you would have to write your own tool as you said which would load the assembly and do validation.

Also, these ideas might help:

  1. You can place the Category attribute on the TestFixture level, this way it will be a bit easier to remember about it and do the validation

  2. You can create custom attributes derived from the Category attribute. This way its easier to ensure that there are no spelling mistakes in category names

See: http://www.nunit.org/index.php?p=category&r=2.4.8

Grzenio
Thanks @Grzenio, I've gone down the reflection route and implemented a test as @Albin suggested in his comment - it works nicely and opens the door for more static analysis. Thanks for the tips also - I've been using string constants up to now for category names, but I may look at introducing new attributes like you said.
Alex Humphrey