Skip to content

Commit 0f9c2b9

Browse files
SeanKilleenstevenaw
andcommitted
Updated Timeout and MaxTime attributes (#1140)
* updated Timeout attribute * updated maxtime * updated maxtime * Update docs/articles/nunit/writing-tests/attributes/maxtime.md Co-authored-by: Steven Weerdenburg <stevenaw@users.noreply.github.com> * Update docs/articles/nunit/writing-tests/attributes/timeout.md Co-authored-by: Steven Weerdenburg <stevenaw@users.noreply.github.com> * Update docs/articles/nunit/writing-tests/attributes/timeout.md Co-authored-by: Steven Weerdenburg <stevenaw@users.noreply.github.com> * Update docs/articles/nunit/writing-tests/attributes/timeout.md Co-authored-by: Steven Weerdenburg <stevenaw@users.noreply.github.com> * Update docs/articles/nunit/writing-tests/attributes/timeout.md * Update docs/articles/nunit/writing-tests/attributes/timeout.md --------- Co-authored-by: Steven Weerdenburg <stevenaw@users.noreply.github.com> Co-authored-by: Sean Killeen <SeanKilleen@gmail.com> 19e4d50
1 parent 6e3c5f2 commit 0f9c2b9

4 files changed

Lines changed: 745 additions & 727 deletions

File tree

articles/nunit/writing-tests/attributes/maxtime.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,15 @@ <h2 id="notes">Notes</h2>
103103
<li><p>This attribute does not cancel the test if the time
104104
is exceeded. It merely waits for the test to complete and then
105105
compares the elapsed time to the specified maximum. If you want to
106-
cancel long-running tests, see <a href="timeout.html">Timeout Attribute</a>.</p>
106+
cancel long-running tests, see <a href="cancelafter.html">CancelAfter Attribute</a>.
107+
The <a href="timeout.html">Timeout Attribute</a> only works for .NET Framework and will give a test error if used for .NET.</p>
107108
</li>
108109
</ol>
110+
<h2 id="see-also">See Also</h2>
111+
<ul>
112+
<li><a href="cancelafter.html">CancelAfter Attribute</a></li>
113+
<li><a href="timeout.html">Timeout Attribute</a></li>
114+
</ul>
109115

110116
</article>
111117
</div>

articles/nunit/writing-tests/attributes/timeout.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,20 @@ <h1 id="timeout">Timeout</h1>
8888

8989
<div class="NOTE">
9090
<h5>Note</h5>
91-
<p>From version 3.12 this is also available in the .NET Standard 2.0 builds of the framework.</p>
91+
<p>The Timeout attribute does not work from .NET 5 and upwards due to limitations in the runtime.
92+
Beginning in NUnit version 4.5, usage of Timeout attribute on .NET 5 or higher is reported as a failure.</p>
9293
</div>
94+
<h2 id="alternatives-to-the-timeout-attribute-for-net-5-and-above">Alternatives to the Timeout attribute for .net 5 and above</h2>
95+
<p>If you want to cancel the Test in the same manner, use the <a href="cancelafter.html">CancelAfter Attribute</a>.
96+
It is cooperative cancelling, so your test needs to handle the CancellationToken.</p>
97+
<p>If you just want to be informed of tests that have run over an expected time, use the <a href="maxtime.html">MaxTime Attribute</a>.</p>
98+
<p>If you want to cancel the whole test run use the <code>dotnet test --blame-hang-timeout &lt;TIMESPAN&gt;</code>.
99+
Any test that use more than the TIMESPAN will abort the run.
100+
See <a href="https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-test-vstest">dotnet test docs</a>.</p>
101+
<h3 id="reason">Reason</h3>
102+
<p>The Timeout attribute uses the <code>Thread.Abort()</code> method to kill tests. <code>Thread.Abort()</code> was removed in .NET 5 and replaced with
103+
cooperative cancellation.</p>
104+
<h2 id="for-projects-that-target-net-framework-only">For projects that target .NET Framework only</h2>
93105
<p>Normally, NUnit simply runs tests and waits for them to terminate -- the test is allowed to run indefinitely. For
94106
certain kinds of tests, however, it may be desirable to specify a timeout value.</p>
95107
<p>The <strong>TimeoutAttribute</strong> is used to specify a timeout value in milliseconds for a test case. If the test case runs

index.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3057,7 +3057,7 @@
30573057
"articles/nunit/writing-tests/attributes/maxtime.html": {
30583058
"href": "articles/nunit/writing-tests/attributes/maxtime.html",
30593059
"title": "MaxTime | NUnit Docs",
3060-
"summary": "MaxTime The MaxTimeAttribute is used on test methods to specify a maximum time in milliseconds for a test case. If the test case takes longer than the specified time to complete, it is reported as a failure. Example [Test, MaxTime(2000)] public void TimedTest() { /* ... */ } Notes Any assertion failures take precedence over the elapsed time check. This attribute does not cancel the test if the time is exceeded. It merely waits for the test to complete and then compares the elapsed time to the specified maximum. If you want to cancel long-running tests, see Timeout Attribute."
3060+
"summary": "MaxTime The MaxTimeAttribute is used on test methods to specify a maximum time in milliseconds for a test case. If the test case takes longer than the specified time to complete, it is reported as a failure. Example [Test, MaxTime(2000)] public void TimedTest() { /* ... */ } Notes Any assertion failures take precedence over the elapsed time check. This attribute does not cancel the test if the time is exceeded. It merely waits for the test to complete and then compares the elapsed time to the specified maximum. If you want to cancel long-running tests, see CancelAfter Attribute. The Timeout Attribute only works for .NET Framework and will give a test error if used for .NET. See Also CancelAfter Attribute Timeout Attribute"
30613061
},
30623062
"articles/nunit/writing-tests/attributes/nonparallelizable.html": {
30633063
"href": "articles/nunit/writing-tests/attributes/nonparallelizable.html",
@@ -3212,7 +3212,7 @@
32123212
"articles/nunit/writing-tests/attributes/timeout.html": {
32133213
"href": "articles/nunit/writing-tests/attributes/timeout.html",
32143214
"title": "Timeout | NUnit Docs",
3215-
"summary": "Timeout Note From version 3.12 this is also available in the .NET Standard 2.0 builds of the framework. Normally, NUnit simply runs tests and waits for them to terminate -- the test is allowed to run indefinitely. For certain kinds of tests, however, it may be desirable to specify a timeout value. The TimeoutAttribute is used to specify a timeout value in milliseconds for a test case. If the test case runs longer than the time specified it is immediately cancelled and reported as a failure, with a message indicating that the timeout was exceeded. The specified timeout value covers the test setup and teardown as well as the test method itself. Before and after actions may also be included, depending on where they were specified. Since the timeout may occur during any of these execution phases, no guarantees can be made as to what will be run and any of these phases of execution may be incomplete. If only used on a test, once a test has timed out, its teardown methods are executed. The attribute may also be specified on a fixture or assembly, in which case it indicates the default timeout for any subordinate test cases. When using the console runner, it is also possible to specify a default timeout on the command-line. Example [Test, Timeout(2000)] public void PotentiallyLongRunningTest() { /* ... */ } Note When debugging a unit test, i.e. when a debugger is attached to the process, then the timeout is not enforced. See Also MaxTime Attribute CancelAfter Attribute"
3215+
"summary": "Timeout Note The Timeout attribute does not work from .NET 5 and upwards due to limitations in the runtime. Beginning in NUnit version 4.5, usage of Timeout attribute on .NET 5 or higher is reported as a failure. Alternatives to the Timeout attribute for .net 5 and above If you want to cancel the Test in the same manner, use the CancelAfter Attribute. It is cooperative cancelling, so your test needs to handle the CancellationToken. If you just want to be informed of tests that have run over an expected time, use the MaxTime Attribute. If you want to cancel the whole test run use the dotnet test --blame-hang-timeout <TIMESPAN>. Any test that use more than the TIMESPAN will abort the run. See dotnet test docs. Reason The Timeout attribute uses the Thread.Abort() method to kill tests. Thread.Abort() was removed in .NET 5 and replaced with cooperative cancellation. For projects that target .NET Framework only Normally, NUnit simply runs tests and waits for them to terminate -- the test is allowed to run indefinitely. For certain kinds of tests, however, it may be desirable to specify a timeout value. The TimeoutAttribute is used to specify a timeout value in milliseconds for a test case. If the test case runs longer than the time specified it is immediately cancelled and reported as a failure, with a message indicating that the timeout was exceeded. The specified timeout value covers the test setup and teardown as well as the test method itself. Before and after actions may also be included, depending on where they were specified. Since the timeout may occur during any of these execution phases, no guarantees can be made as to what will be run and any of these phases of execution may be incomplete. If only used on a test, once a test has timed out, its teardown methods are executed. The attribute may also be specified on a fixture or assembly, in which case it indicates the default timeout for any subordinate test cases. When using the console runner, it is also possible to specify a default timeout on the command-line. Example [Test, Timeout(2000)] public void PotentiallyLongRunningTest() { /* ... */ } Note When debugging a unit test, i.e. when a debugger is attached to the process, then the timeout is not enforced. See Also MaxTime Attribute CancelAfter Attribute"
32163216
},
32173217
"articles/nunit/writing-tests/attributes/values.html": {
32183218
"href": "articles/nunit/writing-tests/attributes/values.html",

0 commit comments

Comments
 (0)