对基于计时器的应用程序进行单元测试? [英] Unit testing a timer based application?

查看:29
本文介绍了对基于计时器的应用程序进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在用 C# 编写一个简单的、基于计时器的迷你应用程序,它每 k 秒执行 n 次操作.
我正在尝试采用测试驱动的开发风格,因此我的目标是对应用的所有部分进行单元测试.

I am currently writing a simple, timer based mini app in C# that performs an action n times every k seconds.
I am trying to adopt a test driven development style, so my goal is to unit test all parts of the app.

所以,我的问题是:有没有一种好方法可以对基于计时器的类进行单元测试?

So, my question is: Is there a good way to unit test a timer based class?

在我看来,问题是存在很大的风险,即执行测试需要很长时间,因为它们必须等待很长时间才能发生所需的操作.
特别是如果你想要真实的数据(秒),而不是使用框架允许的最小时间分辨率(1 毫秒?).
我正在为操作使用模拟对象,以注册该操作被调用的次数,因此该操作几乎不需要时间.

The problem, as I see it, is that there is a big risk that the tests will take uncomfortably long to execute, since they must wait so and so long for the desired actions to happen.
Especially if one wants realistic data (seconds), instead of using the minimal time resolution allowed by the framework (1 ms?).
I am using a mock object for the action, to register the number of times the action was called, and so that the action takes practically no time.

推荐答案

我所做的是模拟计时器,以及当前系统时间,我的事件可以立即触发,但就被测代码而言担心经过的时间是秒.

What I have done is to mock the timer, and also the current system time, that my events could be triggered immediately, but as far as the code under test was concerned time elapsed was seconds.

这篇关于对基于计时器的应用程序进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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