Test
The Test attribute marks a method inside a test fixture as a test. It is normally used for simple (non-parameterized)
tests but may also be applied to parameterized tests without causing extra test cases to be generated. See
Parameterized Tests for more information.
The test method may be either an instance or a static method.
Test methods may be async; NUnit waits for the method to complete before recording the result. Async test methods
must return Task or Task<T>.
If a test method does not have a valid signature, it is treated as not runnable.
Usage
[Test]
Use named parameters (properties on the attribute) for metadata and for checking a return value.
Properties
| Property | Type | Description | Default |
|---|---|---|---|
Description |
string? |
Descriptive text for the test. Equivalent to applying Description. |
null |
Author |
string? |
Author metadata. Equivalent to applying Author. |
null |
TestOf |
Type? |
Type under test. Equivalent to applying TestOf. |
null |
ExpectedResult |
object? |
Expected return value; NUnit compares it to the method result. Not valid if the test method has parameters. | (unset) |
If the test method returns a value and you set ExpectedResult, NUnit checks equality with the return value.
Applies To
| Test Methods | Test Fixtures (Classes) | Assembly |
|---|---|---|
| ✅ | ❌ | ❌ |
Example
[TestFixture]
public sealed class BasicTests
{
[Test]
public void Add()
{
Assert.That(2 + 2, Is.EqualTo(4));
}
}
[TestFixture]
public sealed class DescriptionTests
{
[Test(Description = "My really cool test")]
public void Add_WithDescriptionOnAttribute()
{
Assert.That(2 + 2, Is.EqualTo(4));
}
[Test]
[Description("My really really cool test")]
public void Add_WithDescriptionAttribute()
{
Assert.That(2 + 2, Is.EqualTo(4));
}
}
[TestFixture]
public sealed class AsyncTests
{
[Test]
public async Task AddAsync()
{
await Task.Delay(1);
Assert.That(2 + 2, Is.EqualTo(4));
}
}
[TestFixture]
public sealed class ExpectedResultTests
{
[Test(ExpectedResult = 4)]
public int TestAdd()
{
return 2 + 2;
}
[Test(ExpectedResult = 4)]
public async Task<int> TestAddAsync()
{
await Task.Delay(1);
return 2 + 2;
}
}