c# - 如何使陷入无限循环的测试失败?

标签 c# .net unit-testing .net-2.0 mstest

我有一些产生无限循环的代码。现在我需要编写一个将在大约 200 毫秒后失败的测试。 200ms表示代码进入死循环。

例如:

public void CodeUnderTest()
{
   while(true)
   {

   }
} 

最佳答案

使用 MSTest 时,您可以使用属性

[TestMethod]
[Timeout(200)]

参见 How to force tests to stop running

You can set time limits with which the execution of a test or a test run will comply. You might need to do this, for example, if you work in a test lab and need a test run to complete by a certain time of day.

Another scenario for the use of time limits is that of non-responsive code.

关于c# - 如何使陷入无限循环的测试失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12145510/

相关文章:

c# - WPF如何根据窗口大小调整某个控件的大小?

java - TestNG 中的多级设置/拆卸?

unit-testing - 单元测试应该模拟所有外部服务吗?

c# - .NET Framework 2.0 与 3.5/4.0...使用旧版本有什么优势吗?

c# - SMTP 邮件不发送第二次调用

c# - 在我的 C# 应用程序中处理异常的正确位置在哪里?

.net - 如何在 ASP.NET AJAX Control Toolkit 中的 AsyncFileUpload 的文件浏览菜单中限制文件类型

unit-testing - Postman 不接受 `-` 作为模拟服务器请求的 URI 参数

c# - 在 IIS .net 核心 wwwroot 上找不到 CSS 文件夹

c# - 一行异步方法 SynchronizationContext