翻译 Secrets of the JavaScript Ninja 边译边学(9)

Secrets of the JavaScript Ninja 边译边学(9)

2.3 Testing frameworks
2.3 测试框架

A test suite should serve as a fundamental part of your development workflow. For this reason you should pick a suite that that works particularly well for you your coding style, and your code base.
测试套件也应该成为您的开发工作的基本部分。因此,你应该根据您的代码风格和代码基础选择合适的测试套件。
  A JavaScript test suite should serve a single need: display the result of the tests, making it easy to determine which tests have passed or failed. Testing frameworks can help us reach that goal without us having to worry about anything but creating the tests and organizing them into suites.
  一个JavaScript测试套件应该只满足一个需求:展示所有的测试结果,让人一眼就能看出来哪个测试通过了哪个失败了。测试框架可以帮助我们实现这个目标,而在这个过程中我们只需要关注创建测试用例并且把它们输入套件即可。
  There are a number of features that we might want to look for in a JavaScript unit-testing framework, depending upon the needs of the tests. Some of these features include:
  根据测试需求的不同,我们需要JavaScript单元测试具有的特性也不相同。这些特性包括:
  • The ability to simulate browser behavior (clicks, key presses, an so on).
  • Interactive control of tests (pausing and resuming tests).
  • Handling asynchronous test time outs.
  • The ability to filter which tests are to be executed.
  • 模拟浏览器操作的能力(点击,按下键盘等等)。
  • 测试过程中的人为干预操作(暂停和继续测试)。
  • 处理异步测试超时。
  • 过滤要执行的测试。
  In mid-2009 a survey was conducted, attempting to determine what JavaScript testing frameworks people used in their day-to-day development. The results were quite illuminating.
  在2009年年中,有一项调查试图确认那些JavaScript测试框架是人们在每天的开发工作中经常使用的。结果发人深思。
  The raw results, should you be interested, can be found at
http://spreadsheets.google.com/pub?key=ry8NZN4-Ktao1Rcwae-9Ljw&output=html, and the charted results are as shown in figures 2.5, 2.6 and 2.7.
  你可能关注的是原始结果,在http://spreadsheets.google.com/pub?key=ry8NZN4-Ktao1Rcwae-9Ljw&output=html,图2.5,2.6,2.7展示了其图表化的结果:

  The first figure depicts the disheartening fact that a lot of the respondents don’t test at all. In the wild, it’s easy to believe that the percentage of non-testers is actually quite higher.
  第一个图表说明一个令人沮丧的事实:许多受访者根本不测试。这很容易说明,在更宽泛的意义上,不测试的比例实际上更高。
  Another insight from the results is that the vast majority of scrupt authors that do write tests use one of four tools, all of which were pretty much tied in the results: JSUnit, QUnit, Selenium, and YUITest. The top ten “winners” are shown in figure 2.6.
  另一个结论是:大部分受访者使用四种测试工具的其中之一:JSUnit,QUnit,Selenium,和YUITest。图表2.6中显示了“前十名”测试工具。

  An interesting result, showing that there isn’t any one definitive preferred testing framework at this point. But even more interesting is the massive “long tail” of one-off frameworks that have one, or very few, users as shown in figure 2.7.
  这里有一个有趣的结论显示当前没有任何一个测试工具是大家最喜爱的,但是更有趣的是,如图2.7所示,这幅图有一个“很长的尾巴”展示了存在很多不常使用,甚至只用过一次的测试工具。

  It should be noted that it’s fairly easy for someone to write a testing framework from scratch, and that’s not a bad way to help him or her to gain a greater understanding of what a testing framework is trying to achieve. This is an especially interesting exercise to tackle because, when writing a testing framework, typically we’d be dealing with pure JavaScript without having to worry much about dealing with many cross-browser issues. Unless, that is, you’re trying to simulate browser events, then good luck!
  值得注意的是:对于某人来说,从头开始编写一套测试框架并不容易,但这却对他理解测试框架如何实现很有帮助。这是一个特别有趣的练习,因为当我们编写测试框架时,通常需要面对的是纯JavaScript问题,不需要考虑太多关于跨浏览器的问题。除非,你要试图模拟浏览器事件,如果是那样的话,祝你好运!
  Obviously, according to the result depicted in figure 2.7, a number of people have come to this same conclusion and have written a large number of one-off frameworks to suite their own particular needs.
  根据图表2.7所示的结果不难推出,很多人得出了相同的结论,并且写了很多一次性的测试套件去适应他们自己特殊的需求。

  General JavaScript unit testing frameworks tend to provide a few basic components: a test runner, test groupings, and assertions. Some also provide the ability to run tests asynchronously.
  一般JavaScript单元测试框架都会提供一些基本组件:测试运行工具,测试分组,和断言,有些还会提供异步运行测试的功能。

  But while it is quite easy to write a proprietary unit-testing framework, it’s likely that we’ll just want to use something that’s been pre-built. Let’s take a brief survey of some of the most popular unit testing frameworks.
  但是尽管编写一套专有的单元测试框架很容易,但是我们还是很可能去使用一些已经存在的框架。让我们对一些最流行的单元测试框架做一个简短的调查。

2.3.1 QUnit
QUnit is the unit-testing framework that was originally built to test jQuery. It has since expanded beyond its initial goals and is now a standalone unit-testing framework. QUnit is primarily designed to be a simple solution to unit testing, providing a minimal, but easy to use, API.
QUnit是一个单元测试框架,它最开始用于测试JQuery。但是现在它已经超越了原来的目标,成为了一个独立的单元测试框架。QUnit的主要设计目标是一个简单的单元测试解决方案,提供体积最小但很容易使用的API。

  Distinguishing features:
  特点

  • Simple API

  • Supports asynchronous testing.

  • Not limited to jQuery or jQuery-using code

  • Especially well-suited for regression testing

  • 简单的API

  • 支持异步测试.

  • 不限于JQuery或者使用JQuery的代码

  • 尤其适合做回归测试

More information can be found at http://docs.jquery.com/Qunit
更多信息请访问:http://docs.jquery.com/Qunit

2.3.2 YUITest
YUITest is a testing framework built and developed by Yahoo! and released in October of 2008. It was completely rewritten in 2009 to coincide with the release of YUI 3. YUITest provides an impressive number of features and functionality that is sure to cover any unit testing case required by your code base.
YUITest是由Yahoo开发和创建的测试框架,发布于2008年十月。2009年为适应YUI 3的发布进行彻底地重写。YUITest提供了大量的特性和功能,可以完全覆盖您的代码所需要的任何单元测试用例。

  Distinguishing features:
  特点

• Extensive and comprehensive unit testing functionality
• Supports asynchronous tests
• Good event simulation

• 广泛而全面的单元测试功能
• 支持异步测试
• 良好的事件模拟

More information is available at http://developer.yahoo.com/yui/3/test/
更多信息请访问:http://developer.yahoo.com/yui/3/test/

2.3.3 JSUnit
JSUnit is a port of the popular Java JUnit testing framework to JavaScript. While it’s still one of the most popular JavaScript unit testing frameworks around, JSUnit is also one of the oldest (both in terms of the code base age and quality). The framework hasn’t been updated much recently, so for something that’s known to work with all modern browsers, JSUnit may not be the best choice.
SUnit是JavaScript流行的JavaJUnit测试框架的一个端口。虽然它仍然是当前最流行的JavaScript单元测试框架之一,但JSUnit也是最古老的测试框架之一(从内核代码年限和质量来看)。这个框架最近没有太多更新,所以对于所有的现代浏览器来说,JSUnit可能不是最好的选择。

  More information can be found at http://www.jsunit.net/
  更多信息请访问:http://www.jsunit.net/
  Next, we’ll take a look at creating test suites.
  接下来,我们了解如何创建测试套件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值