Skip to content

Commit a451901

Browse files
committed
Add cancel after snippets
1 parent 46b0b0f commit a451901

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,11 @@ 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+
[!code-csharp[TestWithCancellationToken](~/snippets/Snippets.NUnit/Attributes/CancelAfterAttributeExamples.cs#TestWithCancellationToken)]
4941

42+
[!code-csharp[TestCaseSourceWithCancellationToken](~/snippets/Snippets.NUnit/Attributes/CancelAfterAttributeExamples.cs#TestCaseSourceWithCancellationToken)]
43+
44+
```csharp
5045
[CancelAfter(2000)]
5146
[TestCase("http://server1")]
5247
[TestCase("http://server2")]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
10+
[Test, CancelAfter(2_000)]
11+
public void RunningTestUntilCanceled(CancellationToken cancellationToken)
12+
{
13+
while (!cancellationToken.IsCancellationRequested)
14+
{
15+
/* */
16+
}
17+
}
18+
19+
#endregion
20+
21+
#region TestCaseSourceWithCancellationToken
22+
23+
private static int[] _simpleValues = { 2, 4, 6, 8 };
24+
25+
[TestCaseSource(nameof(_simpleValues)), CancelAfter(1_000)]
26+
public async Task TestCaseSourceWithCancellationToken(int a, CancellationToken cancellationToken)
27+
{
28+
Assert.That(cancellationToken, Is.Not.Default);
29+
30+
while (!cancellationToken.IsCancellationRequested)
31+
{
32+
/* */
33+
}
34+
}
35+
36+
#endregion
37+
}
38+
}

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)