Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/articles/nunit/writing-tests/attributes/maxtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ MaxTimeAttribute(int milliseconds)
|-----------|------|-------------|
| `milliseconds` | `int` | The maximum elapsed time in milliseconds. If the test exceeds this time, it fails. |

## Named Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `WarningTime` | `int` | An optional warning threshold in milliseconds. If the test takes longer than this time but less than the maximum time, the test result is set to `Warning`. A value of `0` (the default) disables the warning threshold. (**NUnit 5+**) |

## Applies To

| Test Methods | Test Fixtures (Classes) | Assembly |
Expand All @@ -34,13 +40,18 @@ MaxTimeAttribute(int milliseconds)

[!code-csharp[MaxTimeVsAssertions](~/snippets/Snippets.NUnit/Attributes/MaxTimeAttributeExamples.cs#MaxTimeVsAssertions)]

### Warning Threshold

[!code-csharp[MaxTimeWarningThreshold](~/snippets/Snippets.NUnit/Attributes/MaxTimeAttributeExamples.cs#MaxTimeWarningThreshold)]

## Notes

1. Any assertion failures take precedence over the elapsed time check. If a test both fails an assertion and exceeds the time limit, the assertion failure is reported.
2. This attribute does **not** cancel or abort the test if the time is exceeded. It waits for the test to complete, then compares the elapsed time to the maximum.
3. For tests that need to be cancelled when they exceed a time limit, use [CancelAfter Attribute](xref:attribute-cancelafter) instead.
4. The timing includes the test method execution only, not `SetUp` or `TearDown` methods.
5. The [Timeout Attribute](xref:attribute-timeout) uses `Thread.Abort` and only works on .NET Framework.
6. The optional `WarningTime` named parameter (added in **NUnit 5+**) sets a soft threshold: if the test exceeds it but still passes within the maximum time, the result is `Warning` rather than `Failure`.

## See Also

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,20 @@ public void AssertionFailuresTakePrecedence()
}
}
#endregion

#region MaxTimeWarningThreshold
[TestFixture]
public class MaxTimeWarningThresholdTests
{
[Test]
[MaxTime(1000, WarningTime = 200)]
public void OperationWithWarningThreshold()
{
// Test passes if it completes within 1000ms.
// If it takes more than 200ms but less than 1000ms, the result is Warning.
Thread.Sleep(201);
}
}
#endregion
}
}
2 changes: 1 addition & 1 deletion docs/snippets/Snippets.NUnit/Snippets.NUnit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.5.1" />
<PackageReference Include="NUnit" Version="4.6.0-*" />
<PackageReference Include="NUnit" Version="5.0.0-alpha.100.7" />
<PackageReference Include="NUnit3TestAdapter" Version="6.2.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.13.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
Loading