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
21 changes: 12 additions & 9 deletions docs/articles/nunit/writing-tests/attributes/cancelafter.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@ When used on test methods, NUnit automatically adds an extra argument to your me

## Example

```csharp
[Test, CancelAfter(2000)]
public void RunningTestUntilCanceled(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
/* */
}
}
The `CancelAfterAttribute` supports cancellation across a variety of ways to write tests.

A simple test, written using the `Test` attribute:

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

A parameterized test written using the `TestCase` attribute:

```csharp
[CancelAfter(2000)]
[TestCase("http://server1")]
[TestCase("http://server2")]
Expand All @@ -60,6 +59,10 @@ public async Task PotentiallyLongRunningTest(string uri, CancellationToken token
}
```

A parameterized test written using the `TestCaseSource` attribute:

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

> [!NOTE]
> When debugging a unit test, i.e. when a debugger is attached to the process, the timeout is not enforced.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using NUnit.Framework;
#pragma warning disable NUnit1029 // Pending https://github.com/nunit/nunit.analyzers/issues/957

namespace Snippets.NUnit.Attributes
{
public class CancelAfterAttributeExamples
{
#region TestWithCancellationToken
[Test, CancelAfter(2_000)]
public void RunningTestUntilCanceled(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
/* */
}
}
#endregion

#region TestCaseSourceWithCancellationToken
private static int[] _simpleValues = { 2, 4, 6, 8 };

[TestCaseSource(nameof(_simpleValues)), CancelAfter(1_000)]
public void TestCaseSourceWithCancellationToken(int a, CancellationToken cancellationToken)
{
Assert.That(cancellationToken, Is.Not.Default);

while (!cancellationToken.IsCancellationRequested)
{
/* */
}
}
#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.0.1" />
<PackageReference Include="NUnit" Version="4.5.0-alpha.0.20" />
<PackageReference Include="NUnit" Version="4.5.0-alpha.0.25" />
<PackageReference Include="NUnit3TestAdapter" Version="6.0.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.11.2">
<PrivateAssets>all</PrivateAssets>
Expand Down
Loading