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() { /* ... */ } }
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.
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.
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 { Critical, Major, Normal, Minor } [AttributeUsage(AttributeTargets.Method, AllowMultiple=false)] public class SeverityAttribute : PropertyAttribute { public SeverityAttribute( SeverityLevel level ) : base( level ); } ... [Test, Severity( SeverityLevel.Critical)] public void MyTest() { /*...*/ }