PropertyAttribute (NUnit 2.4)

The Property attribute provides a generalized approach to setting named properties on any test case or fixture, using a name/value pair.

In the example below, the fixture class MathTests is given a Location value of 723 while the test case AdditionTest is given a Severity of "Critical"


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

  [TestFixture, Property("Location",723)]
  public class MathTests
    [Test, Property("Severity", "Critical")]
	public void AdditionTest()
    { /* ... */ }
Imports System
Imports Nunit.Framework

Namespace Nunit.Tests

  <TestFixture(), Property("Location",723)>
    Public Class MathTests
    <Test(), Property("Severity","Critical")>
	  Public Sub AdditionTest()
    ' ...
    End Sub
  End Class
End Namespace
#using <Nunit.Framework.dll>
using namespace System;
using namespace NUnit::Framework;

namespace NUnitTests
  [TestFixture, Property("Location",723)]
  public __gc class MathTests
    [Test, Property("Severity","Critical")] void AdditionTest();

#include "cppsample.h"

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

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

/** @attribute NUnit.Framework.TestFixture() */
/** @attribute NUnit.Framework.Property("Location",723) */
public class MathTests
  /** @attribute NUnit.Framework.Test() */
  /** @attribute NUnit.Framework.Property("Severity","Critical") */
  public void AdditionTest()
  { /* ... */ }

Usage Note

The PropertyAttribute is not used for any purpose by NUnit itself, but it does display them in the XML output file and in the Test Properties dialog of the gui.

It is possible to write extensions that access the value of specific properties. It is also possible to access the value of properties from within a test using reflection.

Custom Property Attributes

Users can define custom attributes that derive from PropertyAttribute and have them recognized by NUnit. PropertyAttribute provides a protected constructor that takes the value of the property and sets the property name to the name of the derived class. NUnit itself uses this facility: some of it's specialized attributes are actually specializations of PropertyAttribute.

Here's an example that creates a Severity property. It works just like any other property, but has a simpler syntax and is type-safe. A test reporting system might make use of the property to provide special reports.

public enum SeverityLevel

[AttributeUsage(AttributeTargets.Method, AllowMultiple=false)]
public class SeverityAttribute : PropertyAttribute
    public SeverityAttribute( SeverityLevel level )
	    : base( level ); 


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

Beginning with NUnit 2.5, a property attribute is able to contain multiple name/value pairs. This capability is not exposed publicly but may be used by derived property classes. NUnit uses this feature itself for certain attributes. See, for example, RequiresThreadAttribute.