Property
PropertyAttribute provides a generalized approach to setting named properties on any test case or fixture, using a name/value pair. Properties can be used for test filtering, reporting, and custom test organization.
Constructors
PropertyAttribute(string propertyName, string propertyValue)
PropertyAttribute(string propertyName, int propertyValue)
PropertyAttribute(string propertyName, double propertyValue)
| Parameter | Type | Description |
|---|---|---|
propertyName |
string |
The name of the property. |
propertyValue |
string, int, or double |
The value of the property. |
Properties
| Property | Type | Description |
|---|---|---|
Properties |
IPropertyBag |
Gets the property dictionary for this attribute. |
Applies To
| Test Methods | Test Fixtures (Classes) | Assembly |
|---|---|---|
| ✅ | ✅ | ✅ |
Note
When applied to a fixture or assembly, the property is applied to all tests contained within that fixture or assembly.
Multiple PropertyAttribute instances can be applied to the same element (AllowMultiple = true).
Examples
Basic Usage
[TestFixture]
[Property("Location", 723)]
public class MathTests
{
[Test]
[Property("Severity", "Critical")]
public void AdditionTest()
{
Assert.That(2 + 2, Is.EqualTo(4));
}
[Test]
[Property("Severity", "Normal")]
[Property("Author", "John Doe")]
public void SubtractionTest()
{
// Multiple properties can be applied to a single test
Assert.That(5 - 3, Is.EqualTo(2));
}
}
Different Property Value Types
[TestFixture]
public class PropertyTypesTests
{
[Test]
[Property("StringProp", "value")]
[Property("IntProp", 42)]
[Property("DoubleProp", 3.14)]
public void TestWithDifferentPropertyTypes()
{
// Properties can be string, int, or double
Assert.Pass();
}
}
Accessing Properties in Tests
[TestFixture]
public class AccessingPropertiesTests
{
[Test]
[Property("Priority", "High")]
public void TestAccessingProperty()
{
// Access properties via TestContext
var priority = TestContext.CurrentContext.Test.Properties.Get("Priority");
Assert.That(priority, Is.EqualTo("High"));
}
}
Custom Property Attributes
You can define custom attributes that derive from PropertyAttribute for type-safe, domain-specific properties:
// Define a custom severity level enum
public enum SeverityLevel
{
Critical,
Major,
Normal,
Minor
}
// Create a custom property attribute
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false)]
public class SeverityAttribute : PropertyAttribute
{
public SeverityAttribute(SeverityLevel level)
: base(level.ToString())
{
}
}
[TestFixture]
public class CustomPropertyTests
{
[Test]
[Severity(SeverityLevel.Critical)]
public void CriticalTest()
{
// The property name is "Severity" (derived from attribute name)
var severity = TestContext.CurrentContext.Test.Properties.Get("Severity");
Assert.That(severity, Is.EqualTo("Critical"));
}
[Test]
[Severity(SeverityLevel.Minor)]
public void MinorTest()
{
var severity = TestContext.CurrentContext.Test.Properties.Get("Severity");
Assert.That(severity, Is.EqualTo("Minor"));
}
}
Notes
PropertyAttributeis not used by NUnit itself for test execution. Properties are displayed in XML output and the Test Properties dialog.- You can use properties with the
--whereoption on the command-line to select tests to run. See Test Selection Language. Filtering only works for properties withstringvalues. - Tests can access properties through TestContext using
TestContext.CurrentContext.Test.Properties. - When creating custom property attributes, the property name is derived from the class name with the "Attribute" suffix removed (e.g.,
SeverityAttributebecomes property name"Severity").