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

发布于 2024-11-06 12:14:06 字数 265 浏览 0 评论 0原文

这个问题突然出现在我的脑海里,我想在这里问这个问题。

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

我问这个是因为,这种情况可能发生在代码中的任何地方。假设我的方法委托给其他几个方法,我想知道

  • 它是如何陷入无限循环的,
  • 哪组输入导致了它
  • 调用哪个方法(从此方法)导致了这种情况,

我没有为此编写代码。这个问题纯粹是为了了解如果将来出现这种情况该怎么办。请回复。

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

  • How it ran into an infinite loop
  • What set of input caused it
  • Call to which method (from this method) has caused this

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤星 2024-11-13 12:14:06

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

您可以测试几乎相反的情况:“如何对方法进行单元测试,以便该方法对于某些输入的运行时间不会超过 Xxxx 毫秒”。如果此测试失败,您可能已经找到了具有无限循环的候选者。

NUnit 2.5 有一个 TimeoutAttribute,如果测试失败,则测试失败花费的时间比给定的毫秒数要长。

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

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 has a TimeoutAttribute that makes a test fail if the test takes longer than the given amount of milliseconds.

月竹挽风 2024-11-13 12:14:06

我希望你指的是某种消息泵/事件处理循环。 (大多数无限循环都是不好的)。

我会确保循环委托给某个处理输入的类。彻底测试这个类。

循环构造失败的可能性很小。所以我会通过验收测试或手动测试这一点。

这有点类似于测试可执行文件的 Main 函数。这里的技巧还在于确保 Main 委托给可测试的类。

I hope you mean some kind of message-pump/event-handling loop. (Most infinite loops are bad).

I'd ensure that the loop delegates to some class which processes the input. Test this class thoroughly.

The probability of a loop construct failing to work is minimal.. So I'd test this out via an acceptance test or manually.

This is somewhat similar to testing the Main function of an executable. The trick here is also to ensure that Main delegates to a testable class.

要走干脆点 2024-11-13 12:14:06

让循环的函数将循环检查委托给注入的依赖项:

interface IShouldLoop
{
    bool ShouldLoop();
}

class ClassToTest
{
    private final IShouldLoop shouldLoop;

    public ClassToTest(IShouldLoop shouldLoop)
    {
        this.shouldLoop = shouldLoop;
    }

    public void MethodToTest()
    {
        while(shouldLoop.ShouldLoop())
        {
            // do whatever
        }
    }
}

您可以测试它每次都将循环检查委托给 IShouldLoop 依赖项,并且您可以控制测试中的循环,以便它仅循环您想要的次数。在生产环境中,使用始终返回 true 的 IShouldLoop 实例对其进行实例化。

Have the function that loops delegate the loop check to an injected dependency:

interface IShouldLoop
{
    bool ShouldLoop();
}

class ClassToTest
{
    private final IShouldLoop shouldLoop;

    public ClassToTest(IShouldLoop shouldLoop)
    {
        this.shouldLoop = shouldLoop;
    }

    public void MethodToTest()
    {
        while(shouldLoop.ShouldLoop())
        {
            // do whatever
        }
    }
}

You can test that it delegates the loop check to the IShouldLoop dependency every time, and you can control the looping in your test, so that it only loops the number of times you want. In the production environment, instantiate it with an instance of IShouldLoop that always returns true.

冷情 2024-11-13 12:14:06

单元测试的目的是测试程序中每个最简单的单元。

最简单的单元通常是执行单个任务的函数,因此为了统一无限循环,您必须在可以单独调用的单独函数中提取此循环可以执行的每个单个任务,一旦完成此操作,您将能够调用这些函数并给定它们所有可能的参数,以便测试您的函数应该能够处理的不同场景。无限循环作为一个函数不必统一,但它内部的较小任务必须统一。

Purpose of unitesting is to test each most simple unit in your program.

Most simple unit is usually a function which does a single mission, so in order to unitest your infinite Loop you will have to extract each single mission this Loop could do in a seperated function which can be called alone, once done this you will be able to call these functions given them all possible parameter in order to test different szenarios your function should be able to handle. The infinite Loop as a function doesnt have to be unitested, but the smaller missions inside of it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文