Reputation: 6219
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?
Upvotes: 1
Views: 792
Reputation: 46394
Since (at least) nUnit v2.9.1 the CategoryAttribute
(which is used to assign categories to nUnit tests) supports being assigned to entire assemblies.
Setting this attribute will apply the specified category to all tests in the assembly / project. Typically this is done in AssemblyInfo.cs
, however technically it can be done in any codefile.
Example:
using NUnit.Framework;
[assembly:Category("Integration")]
The above example applies the Category value of "Integration" to all tests within the project.
Upvotes: 2
Reputation: 36679
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:
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
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
Upvotes: 1