Legacy Documentation. View NUnit 3 Documentation

CategoryAttribute (NUnit 2.2)

The Category attribute provides an alternative to suites for dealing with groups of tests. Either individual test cases or fixtures may be identified as belonging to a particular category. Both the gui and console test runners allow specifying a list of categories to be included in or excluded from the run. When categories are used, only the tests in the selected categories will be run. Those tests in categories that are not selected are not reported at all.

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.

Test Fixture Syntax

namespace NUnit.Tests
{
  using System;
  using NUnit.Framework;

  [TestFixture]
  [Category("LongRunning")]
  public class LongRunningTests
  {
    // ...
  }
}
Imports System
Imports Nunit.Framework

Namespace Nunit.Tests

  <TestFixture(), Category("LongRunning")>
  Public Class LongRunningTests
    ' ...
  End Class
End Namespace
#using <Nunit.Framework.dll>
using namespace System;
using namespace NUnit::Framework;

namespace NUnitTests
{
  [TestFixture]
  [Category("LongRunning")]
  public __gc class LongRunningTests
  {
    // ...
  };
}

#include "cppsample.h"

namespace NUnitTests {
  // ...
}
package NUnit.Tests;

import System.*;
import NUnit.Framework.TestFixture;


/** @attribute NUnit.Framework.TestFixture() */
/** @attribute NUnit.Framework.Category("LongRunning") */
public class LongRunningTests
{
  // ...
}

Test Syntax

namespace NUnit.Tests
{
  using System;
  using NUnit.Framework;

  [TestFixture]
  public class SuccessTests
  {
    [Test]
    [Category("Long")]
    public void VeryLongTest()
    { /* ... */ }
}
Imports System
Imports Nunit.Framework

Namespace Nunit.Tests

  <TestFixture()>
  Public Class SuccessTests
    <Test(), Category("Long")> Public Sub VeryLongTest()
      ' ...
    End Sub
  End Class
End Namespace
#using <Nunit.Framework.dll>
using namespace System;
using namespace NUnit::Framework;

namespace NUnitTests
{
  [TestFixture]
  public __gc class SuccessTests
  {
    [Test][Category("Long")] void VeryLongTest();
  };
}

#include "cppsample.h"

namespace NUnitTests {
  // ...
}
package NUnit.Tests;

import System.*;
import NUnit.Framework.TestFixture;


/** @attribute NUnit.Framework.TestFixture() */
public class SuccessTests
{
  /** @attribute NUnit.Framework.Test() */
  /** @attribute NUnit.Framework.Category("Long") */
  public void VeryLongTest()
  { /* ... */ }
}

Custom Category Attributes

Beginning with NUnit 2.4, it is possible to define custom attributes that derive from CategoryAttribute and have them recognized by NUnit. The default protected constructor of CategoryAttribute sets the category name to the name of your class.

Here's an example that creates a category of Critical tests. It works just like any other category, but has a simpler syntax. A test reporting system might make use of the attribute to provide special reports.

[AttributeUsage(AttributeTargets.Method, AllowMultiple=false)]
public class CriticalAttribute : CategoryAttribute { }

...

[Test, Critical]
public void MyTest()
{ /*...*/ }