翻譯 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.
  接下來,我們瞭解如何創建測試套件。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章