Datapoint
DatapointAttribute is used to mark a field as providing a single data value for Theory tests. When NUnit executes a Theory, it uses all fields of matching type annotated with [Datapoint] to supply argument values.
This attribute is ignored for ordinary tests, including parameterized tests using [TestCase].
Usage
This is a parameterless attribute that can only be applied to fields.
[Datapoint]
Applies To
| Field | Test Methods | Test Fixtures (Classes) | Assembly |
|---|---|---|---|
| ✅ | ❌ | ❌ | ❌ |
Note
The field type must exactly match the Theory parameter type - no implicit conversions are performed.
Examples
Basic Usage
[TestFixture]
public class SquareRootTests
{
// Individual datapoints for double arguments
[Datapoint]
public double Zero = 0;
[Datapoint]
public double Positive = 1;
[Datapoint]
public double Negative = -1;
[Datapoint]
public double Max = double.MaxValue;
[Theory]
public void SquareRootIsPositive(double value)
{
// NUnit will call this theory with each datapoint: 0, 1, -1, MaxValue
Assume.That(value >= 0);
Assert.That(Math.Sqrt(value), Is.GreaterThanOrEqualTo(0));
}
}
With Enums and Automatic Datapoints
public enum Color
{
Red,
Green,
Blue
}
[TestFixture]
public class EnumTheoryTests
{
// No need to define datapoints for enums - NUnit supplies them automatically
[Theory]
public void ColorIsValid(Color color)
{
// NUnit automatically tests with Red, Green, and Blue
Assert.That(Enum.IsDefined(typeof(Color), color), Is.True);
}
// Override automatic enum values with specific datapoints
[Datapoint]
public Color OnlyRed = Color.Red;
[Theory]
public void TestOnlyRed(Color color)
{
// When datapoints are defined, automatic enum values are suppressed
Assert.That(color, Is.EqualTo(Color.Red));
}
}
Multiple Types
[TestFixture]
public class MultipleTypeTests
{
// Datapoints for string arguments
[Datapoint]
public string Empty = "";
[Datapoint]
public string NonEmpty = "hello";
// Datapoints for int arguments
[Datapoint]
public int Zero2 = 0;
[Datapoint]
public int Positive2 = 42;
[Theory]
public void StringLengthMatchesCount(string value)
{
// Only uses string datapoints
Assume.That(value, Is.Not.Null);
Assert.That(value.Length, Is.GreaterThanOrEqualTo(0));
}
[Theory]
public void IntIsNonNegative(int value)
{
// Only uses int datapoints
Assume.That(value >= 0);
Assert.That(value, Is.GreaterThanOrEqualTo(0));
}
}
Automatically Supplied Datapoints
NUnit automatically supplies datapoints for certain types:
| Type | Automatic Values |
|---|---|
bool |
true, false |
Any enum |
All defined enum values |
If you supply any datapoints for a parameter type, automatic datapoint generation for that type is suppressed.
Notes
- Fields must be members of the class containing the Theory.
- The field type must exactly match the parameter type - no implicit conversions are performed.
- Use
DatapointSourcewhen you need to provide multiple values from a single source (arrays, collections, or methods). - Datapoints from multiple fields of the same type are combined.