一个单元如何测试程序或基于事件的代码部分 [英] How does one unit test sections of code that are procedural or event-based

查看:22
本文介绍了一个单元如何测试程序或基于事件的代码部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这个演示文稿中被说服了 和网站上的其他评论,我需要学习单元测试.我也意识到有很多关于这里是什么单元测试的问题.每次我去考虑应该如何在我目前正在处理的应用程序中完成它时,我都会困惑地走开.它是一个 xulrunner 应用程序,很多逻辑都是基于事件的——当用户点击这里时,这个动作就会发生.

I'm convinced from this presentation and other commentary here on the site that I need to learn to Unit Test. I also realize that there have been many questions about what unit testing is here. Each time I go to consider how it should be done in the application I am currently working on, I walk away confused. It is a xulrunner application application, and a lot of the logic is event-based - when a user clicks here, this action takes place.

我看到的测试示例通常是测试类——它们实例化一个对象,给它模拟数据,然后检查对象的属性.这对我来说很有意义 - 但非面向对象的部分呢?

Often the examples I see for testing are testing classes - they instantiate an object, give it mock data, then check the properties of the object afterward. That makes sense to me - but what about the non-object-oriented pieces?

这个人提到那个 GUI在大多数测试框架中,基于 -based 的单元测试都很困难,也许这就是问题所在.上面链接的演示文稿提到每个测试一次只能接触一个类、一个方法.这似乎排除了我想要做的事情.

This guy mentioned that GUI-based unit testing is difficult in most any testing framework, maybe that's the problem. The presentation linked above mentions that each test should only touch one class, one method at a time. That seems to rule out what I'm trying to do.

那么问题 - 一个单元如何测试程序或基于事件的代码?提供指向良好文档的链接,或自行解释.

So the question - how does one unit testing procedural or event-based code? Provide a link to good documentation, or explain it yourself.

顺便说一句,我还面临一个挑战,即尚未找到用于测试 xulrunner 应用程序的测试框架 - 似乎这些工具尚未开发.我想这比我对概念的理解、编写可测试的代码、应用单元测试更外围.

On a side note, I also have a challenge of not having found a testing framework that is set up to test xulrunner apps - it seems that the tools just aren't developed yet. I imagine this is more peripheral than my understanding the concepts, writing testable code, applying unit testing.

推荐答案

单元测试的想法是在每个测试中测试一小段代码.在基于事件的系统中,您可以进行的一种单元测试形式是测试您的事件处理程序如何响应各种事件.所以你的单元测试可能将你程序的某个方面设置为特定状态,然后直接调用事件监听器方法,最后测试你程序的后续状态.

The idea of unit testing is to test small sections of code with each test. In an event based system, one form of unit testing you could do, would be to test how your event handlers respond to various events. So your unit test might set an aspect of your program into a specific state, then call the event listener method directly, and finally test the subsequent state of of your program.

如果您计划对基于事件的系统进行单元测试,那么如果您使用依赖注入模式,并且理想情况下会全程使用控制反转(请参阅 http://martinfowler.com/articles/injection.htmlhttp://msdn.microsoft.com/en-us/library/aa973811.aspx 了解这些模式的详细信息)

If you plan on unit testing an event-based system, you will make your life a lot easier for yourself if you use the dependency injection pattern and ideally would go the whole way and use inversion of control (see http://martinfowler.com/articles/injection.html and http://msdn.microsoft.com/en-us/library/aa973811.aspx for details of these patterns)

(感谢 pc1oad1etter 指出我弄乱了链接)

(thanks to pc1oad1etter for pointing out I'd messed up the links)

这篇关于一个单元如何测试程序或基于事件的代码部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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