如何对某个输入遇到无限循环的方法进行单元测试? [英] How to unit test a method that runs into an infinite loop for some input?

查看:124
本文介绍了如何对某个输入遇到无限循环的方法进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题只在我脑海中浮现,我想在这里问这个问题.

This question just occurred to my mind and I want to ask this here.

这种情况是故意的,我只是编写了一个无限循环的循环.我该如何进行单元测试?

The case is intentional, I just write a loop which runs infinitely. How do I go about unit testing it?

我之所以这样问,是因为这种情况可能发生在代码中的任何地方.说我的方法委托给其他几种方法,我想知道

I ask this because, this situation may occur anywhere in the code. Say my method delegates to several other methods, and I want to know

  • 它如何陷入无限循环
  • 是什么输入引起的?
  • 调用导致此问题的方法(来自此方法)

我没有为此编写代码.这个问题纯粹是出于知识方面的考虑,如果将来出现这种情况该怎么办.请回复.

I have no code written for this. The question is purely for knowledge sake as to what to do if this situation arises in future. Please respond.

推荐答案

如何对某些输入遇到无限循环的方法进行单元测试?

How to unit test a method that runs into an infinite loop for some input?

您可以测试几乎相反的内容:"如何对方法进行单元测试,以使该方法对于某些输入不会运行超过Xxxx毫秒".如果此测试失败,您可能已经找到了一个无限循环的候选者.

You can test the nearly opposite: "How to unit test a method so that the method will not run longer than Xxxx milliseconds for some input". If this test fails you may have found a candidate with an infinite loop.

NUnit 2.5具有 TimeoutAttribute ,这会使测试失败如果测试花费的时间超过了给定的毫秒数.

NUnit 2.5 has a TimeoutAttribute that makes a test fail if the test takes longer than the given amount of milliseconds.

这篇关于如何对某个输入遇到无限循环的方法进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆