|
| 1 | +--- |
| 2 | +uid: cancelafterattribute |
| 3 | +--- |
| 4 | + |
1 | 5 | # CancelAfter |
2 | 6 |
|
3 | 7 | Normally, NUnit simply runs tests and waits for them to terminate -- the test is allowed to run indefinitely. For |
4 | 8 | certain kinds of tests, however, it may be desirable to specify a timeout value. |
5 | 9 |
|
6 | 10 | For .NET Core and later, |
7 | | -[`Thread.Abort`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread.abort?view=net-7.0) as used by the |
8 | | -[`TimeoutAttribue`](./timeout.md) can no longer be used, and there is therefore no way to interrupt an endless loop. |
| 11 | +[`Thread.Abort`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread.abort?view=net-8.0) as used by the |
| 12 | +[`TimeoutAttribute`](./timeout.md) can no longer be used, and there is therefore no way to interrupt an endless loop. |
9 | 13 |
|
10 | 14 | For all tests, one could use the `--blame-hang(-timeout)` options of [`dotnet |
11 | 15 | test`](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-test#options). However, this will stop any further |
12 | 16 | execution of the remaining tests. |
13 | 17 |
|
14 | 18 | To still be able to cancel tests, one has to move to cooperative cancellation. See [Cancellation in Managed |
15 | 19 | Threads](https://learn.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads) using a |
16 | | -[`CancellationToken``](https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken?view=net-7.0). |
| 20 | +[`CancellationToken``](https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken?view=net-8.0). |
17 | 21 |
|
18 | 22 | The `CancelAfterAttribute` is used to specify a timeout value in milliseconds for a test case. If the test case runs |
19 | 23 | longer than the time specified, the supplied `CancellationToken` is set to canceled. It is however up to the test code |
@@ -60,3 +64,6 @@ public async Task PotentiallyLongRunningTest(string uri, CancellationToken token |
60 | 64 | > When debugging a unit test, i.e. when a debugger is attached to the process, the timeout is not enforced. |
61 | 65 |
|
62 | 66 | ## See Also |
| 67 | + |
| 68 | +* [Timeout Attribute](./timeout.md) |
| 69 | +* [MaxTime Attribute](./maxtime.md) |
0 commit comments