Skip to content

Commit ab1e1d6

Browse files
authored
Add cancel after snippets (#1114)
* Add cancel after snippets * Adjust some formatting for cancelafter attribute
1 parent 46b0b0f commit ab1e1d6

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

docs/articles/nunit/writing-tests/attributes/cancelafter.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,15 @@ When used on test methods, NUnit automatically adds an extra argument to your me
3737

3838
## Example
3939

40-
```csharp
41-
[Test, CancelAfter(2000)]
42-
public void RunningTestUntilCanceled(CancellationToken token)
43-
{
44-
while (!token.IsCancellationRequested)
45-
{
46-
/* */
47-
}
48-
}
40+
The `CancelAfterAttribute` supports cancellation across a variety of ways to write tests.
41+
42+
A simple test, written using the `Test` attribute:
43+
44+
[!code-csharp[TestWithCancellationToken](~/snippets/Snippets.NUnit/Attributes/CancelAfterAttributeExamples.cs#TestWithCancellationToken)]
4945

46+
A parameterized test written using the `TestCase` attribute:
47+
48+
```csharp
5049
[CancelAfter(2000)]
5150
[TestCase("http://server1")]
5251
[TestCase("http://server2")]
@@ -60,6 +59,10 @@ public async Task PotentiallyLongRunningTest(string uri, CancellationToken token
6059
}
6160
```
6261

62+
A parameterized test written using the `TestCaseSource` attribute:
63+
64+
[!code-csharp[TestCaseSourceWithCancellationToken](~/snippets/Snippets.NUnit/Attributes/CancelAfterAttributeExamples.cs#TestCaseSourceWithCancellationToken)]
65+
6366
> [!NOTE]
6467
> When debugging a unit test, i.e. when a debugger is attached to the process, the timeout is not enforced.
6568
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using NUnit.Framework;
2+
#pragma warning disable NUnit1029 // Pending https://github.com/nunit/nunit.analyzers/issues/957
3+
4+
namespace Snippets.NUnit.Attributes
5+
{
6+
public class CancelAfterAttributeExamples
7+
{
8+
#region TestWithCancellationToken
9+
[Test, CancelAfter(2_000)]
10+
public void RunningTestUntilCanceled(CancellationToken cancellationToken)
11+
{
12+
while (!cancellationToken.IsCancellationRequested)
13+
{
14+
/* */
15+
}
16+
}
17+
#endregion
18+
19+
#region TestCaseSourceWithCancellationToken
20+
private static int[] _simpleValues = { 2, 4, 6, 8 };
21+
22+
[TestCaseSource(nameof(_simpleValues)), CancelAfter(1_000)]
23+
public void TestCaseSourceWithCancellationToken(int a, CancellationToken cancellationToken)
24+
{
25+
Assert.That(cancellationToken, Is.Not.Default);
26+
27+
while (!cancellationToken.IsCancellationRequested)
28+
{
29+
/* */
30+
}
31+
}
32+
#endregion
33+
}
34+
}

docs/snippets/Snippets.NUnit/Snippets.NUnit.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<ItemGroup>
1212
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
13-
<PackageReference Include="NUnit" Version="4.5.0-alpha.0.20" />
13+
<PackageReference Include="NUnit" Version="4.5.0-alpha.0.25" />
1414
<PackageReference Include="NUnit3TestAdapter" Version="6.0.0" />
1515
<PackageReference Include="NUnit.Analyzers" Version="4.11.2">
1616
<PrivateAssets>all</PrivateAssets>

0 commit comments

Comments
 (0)