Random
RandomAttribute is used to specify a set of random values to be provided for an individual parameter of a parameterized test method. It supports numeric types, Guid, and enums.
Constructors
Count Only (Auto-detect Type)
RandomAttribute(int count)
| Parameter |
Type |
Description |
count |
int |
The number of random values to generate. The type is inferred from the parameter. |
With Range (Type-specific)
RandomAttribute(int min, int max, int count)
RandomAttribute(double min, double max, int count)
// Also available for: uint, long, ulong, short, ushort, byte, sbyte, float
| Parameter |
Type |
Description |
min |
varies |
The minimum value (inclusive). |
max |
varies |
The maximum value (exclusive). |
count |
int |
The number of random values to generate. |
Properties
| Property |
Type |
Description |
Default |
Distinct |
bool |
When true, no value will be repeated among the generated values. |
false |
Applies To
| Method Parameters |
Test Methods |
Test Fixtures (Classes) |
Assembly |
| ✅ |
❌ |
❌ |
❌ |
Supported Types
| Type |
Count-only |
With Range |
int, uint, long, ulong |
Yes |
Yes |
short, ushort, byte, sbyte |
Yes |
Yes |
double, float |
Yes |
Yes |
decimal |
Yes |
No (use int or double range) |
Guid |
Yes |
No |
enum |
Yes |
No |
Examples
Basic Usage
[TestFixture]
public class RandomBasicTests
{
[Test]
public void TestWithRandomInts([Random(5)] int value)
{
// Generates 5 random int values across the full int range
TestContext.Out.WriteLine($"Testing with value: {value}");
Assert.That(value, Is.TypeOf<int>());
}
[Test]
public void TestWithRandomDoubles([Random(3)] double value)
{
// Generates 3 random doubles between 0.0 and 1.0
TestContext.Out.WriteLine($"Testing with value: {value}");
Assert.That(value, Is.InRange(0.0, 1.0));
}
}
With Range
[TestFixture]
public class RandomRangeTests
{
[Test]
public void TestWithIntRange([Random(1, 100, 5)] int value)
{
// Generates 5 random ints between 1 and 100 (exclusive)
Assert.That(value, Is.InRange(1, 99));
}
[Test]
public void TestWithDoubleRange([Random(-1.0, 1.0, 3)] double value)
{
// Generates 3 random doubles between -1.0 and 1.0
Assert.That(value, Is.InRange(-1.0, 1.0));
}
}
Distinct Values
[TestFixture]
public class RandomDistinctTests
{
[Test]
public void TestWithDistinctValues([Random(1, 10, 5, Distinct = true)] int value)
{
// Generates 5 distinct random ints - no value will repeat
Assert.That(value, Is.InRange(1, 9));
}
}
Random GUIDs
[TestFixture]
public class RandomGuidTests
{
[Test]
public void TestWithRandomGuid([Random(3)] Guid value)
{
// Generates 3 random GUIDs
// Note: Guid does not support min/max range
Assert.That(value, Is.Not.EqualTo(Guid.Empty));
}
}
Combined with Other Attributes
[TestFixture]
public class RandomCombinedTests
{
[Test]
public void TestWithMultipleRandomParams(
[Values(1, 2, 3)] int x,
[Random(-1.0, 1.0, 2)] double d)
{
// Creates 6 test cases: 3 x values * 2 random doubles
Assert.That(d, Is.InRange(-1.0, 1.0));
}
}
Notes
- By default, NUnit creates test cases from all combinations of parameter values (combinatorial). Use
[Sequential] to pair values in order instead.
- There is no constructor for
decimal min/max because .NET does not support decimal in attribute constructors. Use an int or double range instead - values will be converted.
Guid does not support min/max ranges - only the count-only constructor works.
- When using
Distinct = true, ensure the range is large enough to provide the requested number of distinct values.
Guid support was added in NUnit 4.0.
See Also