I have the following problem: I'm aware that there is a Timeout attribute in NUnit. My problem is that the unit tests actually spawn a new process which if frozen is never killed by NUnit. How can I implement a timeout which will actually get rid of the rogue process?
我有以下问题:我知道NUnit中有一个Timeout属性。我的问题是单元测试实际上产生了一个新进程,如果冻结从未被NUnit杀死。如何实现超时,实际上将摆脱流氓进程?
Thanks, R.
4 个解决方案
#1
12
You can use timeout for assertion, instead of timeout for whole test method:
你可以使用timeout来断言,而不是整个测试方法的超时:
Assert.That(actual, Is.EqualTo(expected).After(5000, 50));
#2
3
Edit: The accepted answer is a better one.
编辑:接受的答案是更好的答案。
If your test eventually times out (albeit later than you hope), you can always use the MaxTime
attribute: http://www.nunit.org/index.php?p=maxtime&r=2.5.1 and clean up the thread yourself:
如果您的测试最终超时(尽管比您希望的要晚),您可以始终使用MaxTime属性:http://www.nunit.org/index.php?p = maxtime&r = 2.5.1并自行清理线程:
[Test, Maxtime(2000)]
public void TimedTest()
{
...
}
Otherwise, your best option might be to implement your own timing mechanism. Use a timer (or a busy while loop) and if the timeout is exceeded, then kill the process (maybe in the ShutDown method if it's generic across tests) and signal failure.
否则,您最好的选择可能是实现自己的计时机制。使用计时器(或繁忙的while循环)并且如果超时超时,则终止进程(如果在测试中它是通用的,则可能在ShutDown方法中)并且信号失败。
NUnit timeouts don't have any events that allow you to run code after the test times out. You have to roll your own, it looks like.
NUnit超时没有任何允许您在测试超时后运行代码的事件。你必须自己动手,它看起来像。
#3
1
[Test, Timeout(2000)]
public void PotentiallyLongRunningTest()
{
...
}
http://www.nunit.org/index.php?p=timeout&r=2.5.1 :
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.
TimeoutAttribute用于指定测试用例的超时值(以毫秒为单位)。如果测试用例的运行时间超过指定的时间,则会立即取消并报告为失败,并显示超出超时的消息。
The attribute may also be specified on a fixture or assembly, in which case it indicates the default timeout for any subordinate test cases.
该属性也可以在fixture或程序集上指定,在这种情况下,它指示任何从属测试用例的默认超时。
#4
0
OK. After I've tried all the features built in NUnit and failed to achieve what I needed, I did the following: I've added a timer which is being set in a function attributed [SetUp] on a fixture level and thus is called before each Test. If the timer ticks before the test has finished, I just do the clean-up in the timer callback.
好。在我尝试了NUnit中内置的所有功能并且无法实现我所需的功能之后,我做了以下操作:我添加了一个定时器,该定时器在一个函数中设置为[SetUp]属于一个夹具级别,因此在之前被调用每次测试。如果计时器在测试结束前计时,我只是在计时器回调中进行清理。
#1
12
You can use timeout for assertion, instead of timeout for whole test method:
你可以使用timeout来断言,而不是整个测试方法的超时:
Assert.That(actual, Is.EqualTo(expected).After(5000, 50));
#2
3
Edit: The accepted answer is a better one.
编辑:接受的答案是更好的答案。
If your test eventually times out (albeit later than you hope), you can always use the MaxTime
attribute: http://www.nunit.org/index.php?p=maxtime&r=2.5.1 and clean up the thread yourself:
如果您的测试最终超时(尽管比您希望的要晚),您可以始终使用MaxTime属性:http://www.nunit.org/index.php?p = maxtime&r = 2.5.1并自行清理线程:
[Test, Maxtime(2000)]
public void TimedTest()
{
...
}
Otherwise, your best option might be to implement your own timing mechanism. Use a timer (or a busy while loop) and if the timeout is exceeded, then kill the process (maybe in the ShutDown method if it's generic across tests) and signal failure.
否则,您最好的选择可能是实现自己的计时机制。使用计时器(或繁忙的while循环)并且如果超时超时,则终止进程(如果在测试中它是通用的,则可能在ShutDown方法中)并且信号失败。
NUnit timeouts don't have any events that allow you to run code after the test times out. You have to roll your own, it looks like.
NUnit超时没有任何允许您在测试超时后运行代码的事件。你必须自己动手,它看起来像。
#3
1
[Test, Timeout(2000)]
public void PotentiallyLongRunningTest()
{
...
}
http://www.nunit.org/index.php?p=timeout&r=2.5.1 :
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.
TimeoutAttribute用于指定测试用例的超时值(以毫秒为单位)。如果测试用例的运行时间超过指定的时间,则会立即取消并报告为失败,并显示超出超时的消息。
The attribute may also be specified on a fixture or assembly, in which case it indicates the default timeout for any subordinate test cases.
该属性也可以在fixture或程序集上指定,在这种情况下,它指示任何从属测试用例的默认超时。
#4
0
OK. After I've tried all the features built in NUnit and failed to achieve what I needed, I did the following: I've added a timer which is being set in a function attributed [SetUp] on a fixture level and thus is called before each Test. If the timer ticks before the test has finished, I just do the clean-up in the timer callback.
好。在我尝试了NUnit中内置的所有功能并且无法实现我所需的功能之后,我做了以下操作:我添加了一个定时器,该定时器在一个函数中设置为[SetUp]属于一个夹具级别,因此在之前被调用每次测试。如果计时器在测试结束前计时,我只是在计时器回调中进行清理。