用戶端JavaScript代碼單元測試方案

來源:互聯網
上載者:User

Unit testing options for JavaScript

JavaScript單元測試方案

 

Author: Tony Patton
作者:Tony Patton

 

翻譯:PurpleEndurer,2008-08-20 第1版

 

Category: Javascript, Application Development
分類:Javascript, 應用程式開發

 

Tags: JavaScript, Unit, Web Browser, JsUnit JUnit, JUnit, Tests, RhinoUnit RhinoUnit, Dustin Machi, Scripting Languages, Software/Web Development

標籤:JavaScript,單元,網頁瀏覽器,JUnit,JsUnit,測試,RhinoUnit RhinoUnit,Dustin Machi, 指令碼語言,軟體/網頁開發

 

英文出處:http://blogs.techrepublic.com.com/programming-and-development/?p=714&tag=nl.e055

Unit testing is different from traditional testing because it is performed by the developer and not a tester. Tony Patton provides an overview of several options for unit testing client-side JavaScript code.

 

單元測試與傳統測試的不同之處在於它是由開發人員而非測試員來執行的。Tony Patton提供了針對用戶端JavaScript代碼單元測試的多個可選方案的概貌。

 

——————————————————————————————————————-

Testing is an integral aspect of any software development project, but it is not restricted to the stage where a group of testers (who are not involved in application development) bang away on an application to make sure it meets expectations. Developers help ensure code performs as expected by integrating testing into their everyday work with unit testing. Here’s a look at a few options for unit testing client-side JavaScript code.

測試是任何軟體開發工程中的一個不可或缺的方面,但它不限於一群測試員(不參與應用程式開發)集中對一個應有程式挑刺以確保其符合期望的階段。開發人員藉助單元測試將測試整合到其日常工作中,有助於確保代碼照預計執行。下面看幾個選項單元測試用戶端JavaScript代碼。

 

《Purple Endurer註:1、bang away:砰砰地不住射擊》

 

Unit testing 單元測試

 

Unit testing is the testing of individual software components or modules. Unit testing is different from traditional testing because it is performed by the developer and not a tester; this is due to the fact that knowledge of the code is required for unit testing. Later, traditional testing is accomplished with all modules (that have been individually unit tested) working together.

 

單元測試是軟體各組件或模組的測試。單元測試與傳統測試的不同之處在於它是由開發人員而非測試員來執行的;這是由於單元測試需要編寫代碼的知識;稍後的傳統測試是通過所有模組(即已逐個單元測試過)一起工作來完成。

 

《Purple Endurer註:1、due to the fact that:由於》

 

A key feature of unit testing is assertions; these allow the developer to check code execution and variable and object state during unit testing. Assertions allow you to perform common comparisons and condition testing to examine how code is running.

 

單元測試一個關鍵特性是斷言;這允許開發人員在單元測試中檢測代碼執行,變數和目標狀態。斷言允許你執行常規比較和條件測試來檢測代碼如何運行。

 

Many developers don’t recognize JavaScript has real programming, but JavaScript is a full-featured language that has widespread usage given the popularity of AJAX, which relies heavily on JavaScript code. So, how does a developer unit test their JavaScript code? A quick survey of the available JavaScript unit testing options yielded a few options.

 

一些開發人員不認為JavaScript具有真正的程式設計。但JavaScript是一種功能齊全的語言,由於AJAX技術的普及而具有廣泛的使用,AJAX在很大程度上依賴於JavaScript代碼。那麼,開發人員如何單元測試他們的JavaScript代碼呢?一個可用的JavaScript單元測試方案的快速調查問卷帶來了一些選項。

 

JsUnit

 

JUnit is a popular unit-testing tool for Java code. It was developed years ago, and it has become one of the more popular unit testing frameworks for Java developers. JUnit is based on the xUnit framework, which is a set of language-independent concepts and constructs for writing and running unit tests.

 

Junit是一個受歡迎的Java代碼的單元測試工具。它是多年前開發的,並已成為一個較受Java開發人員歡迎的單元測試架構。Junit基於xUnit架構,xUnit這是一套獨立於語言、為編寫和運行單元測試的概念和結構。

 

JUnit was so popular that NUnit was created for the .NET Framework. JavaScript developers can now take advantage of it as well with JsUnit for unit testing client-side JavaScript code. A great feature of these unit testing frameworks is that they are open source.

 

JUnit是如此受歡迎, NUunit則是為.NET Framework打造的。 JavaScript開發人員現在可以利用它配合JsUnit來為用戶端JavaScript代碼做單元測試。這些單元測試架構的一個巨大特點是它們開放原始碼。

 

JsUnit is pure JavaScript; that is, it is written with 100% JavaScript. Like many Web applications, it runs on most browsers. The fact that it runs within the actual browser sets it apart from other testing options. The list of supported browsers includes the following:

 

JsUnit是純JavaScript,也就是說,它100%是用JavaScript寫的。像許多Web應用程式一樣,它運行於絕大多數瀏覽器。
實際上,它運行在真實的瀏覽器中,缺少其他測試選項。JsUnit支援的瀏覽器列表包括以下內容:

 

《Purple Endurer註:1、apart from:離開(除了...之外,缺少,除...以外,且不說)》

 

Internet Explorer 5.5+ on Windows 95/NT/2000/XP and Mac OS 9/X
Mozilla 0.9.4+ on all platforms
Firefox 0.9+ on all platforms
Netscape 6.2.3+ on all platforms
Konqueror 5+ on Linux KDE 3.0.1+
Safari 1.2+ on Max OS X

 

A key aspect of JsUnit is that it is designed to test client-side JavaScript code and nothing else; so, you will not use JsUnit to fully test a Web application.

 

JsUnit的一個關鍵方面是,它旨在測試用戶端的JavaScript代碼,並沒有別的,所以,您將無法使用jsunit來對Web應用程式做全面測試。

 

While JsUnit provides the same functionality as JUnit and NUnit, it utilizes its own vocabulary to describe its features. For example, JsUnit’s unit tests are called Test Functions, which reside in Test Pages (an HTML file). A test runner page is used to run Test Pages.

 

儘管提供了與JUnit和NUnit相同的功能,JsUnit利用自己的詞彙來形容它的功能。舉例來說,JsUnit的單元測試叫做測試函數集,該函數集位於測試網頁(一個 HTML檔案)中 。測試回合頁被用來運行測試頁面。

 

《Purple Endurer註:1、reside in:永久地或長久地居住在》

 

JsUnit is a great tool; however, it has been a couple of years since there were product updates on its SourceForge page.

 

JsUnit是一個偉大的工具;但其在SourceForge網頁上已數年未有產品更新了。

 

《Purple Endurer註:1、It's been years since we saw her.我們已經有好幾年沒有見到她了》

 

Crosscheck

 

Crosscheck is a Java-based tool for unit testing JavaScript code. It is a standalone application that does not run within a browser, so it is a bit hard to realize whether the code actually works in a browser. However, Crosscheck promises to simulate the various environments in which your scripts may run, so it simulates various browsers.

 

Crosscheck是一個基於Java的工具,用於為JavaScript代碼作單元測試。它是一個獨立的應用程式,並不運行在一個瀏覽器內,因此,要瞭解代碼是否能在瀏覽器中工作有點難。不過, Crosscheck承諾會類比您的指令碼可能啟動並執行各種環境,所以它類比了各種瀏覽器。

 

Since the browsers are simulated, you do not have to load the various browsers that you want to test against. Crosscheck’s goal is to provide a mechanism for testing that JavaScript libraries will function as expected.

 

由於瀏覽器可以類比,你不用載入各種想要的瀏覽器來做過敏測試。Crosscheck的目標是提供一個機制來測試JavaScript庫將如預期般運作。

 

Tests are defined in a test file that sets up the tests to run, as well as other functions. It allows you to set up various elements of a test (such as HTML elements) and user actions (like clicking a button) to simulate user interaction. The output of using Crosscheck is a bit rudimentary, as it is generated via the command-line interface.

 

測試定義是在一個測試檔案,在該檔案中設立了待啟動並執行測試,以及功能。它可以讓您設立各種要測試的元素(如HTML元素)和使用者的動作(如點擊一個按鍵)來類比使用者互動。Crosscheck使用的輸出有點簡陋,因為它是通過命令列介面產生。

 

RhinoUnit

 

RhinoUnit is designed to work with ANT utilizing the Rhino engine (JavaScript via Java). It provides full unit testing features, as well as advanced options like ensuring a function is called and generated and checking variable scoping. An interesting feature is its strong integration with JSLint to validate JavaScript code.

 

RhinoUnit被設計來與利用Rhino引擎(通過Java來解釋、運行JavaScript)的ANT配合工作。它提供了全部單元測試功能,和像確保函數被調用、產生和檢查變數範圍的進階選項。一個有趣的特點是其與JSLint的強大整合來驗證JavaScript代碼。

 

Libraries

 

Various JavaScript frameworks include unit testing features. For instance, qooxdoo (which resembles JsUnit) includes a test runner for unit testing its classes. The qooxdoo Test Runner framework can also be used for non-qooxdoo code; it provides a GUI, a layer of infrastructure, and an interface for arbitrary test classes. This allows you to write your own test classes and take advantage of the environment.

 

各種JavaScript架構套件括了單元測試功能。舉例來說, qooxdoo (類似於JsUnit)包括測試回合機制器來單元測試其類。該qooxdoo測試回合器架構也可用於非qooxdoo代碼;它提供了一個GUI(圖形使用者介面),一個基礎設施層,和一個任意實驗類介面。這可以讓你寫自己的實驗類,並利用環境。

 

The Dojo Toolkit also provides a robust unit testing environment. Its testing tool is called the Dojo Objective Harness; it facilitates the use of custom namespaces but requires special steps to utilize the test runner included within the Dojo Toolkit. Dustin Machi’s SitePen blog post provides instructions for making the Dojo Objective Harness work.

 

Dojo工具包提供了強大的單元測試環境。其測試環境稱為Dojo對象甲胄;它有助於使用者名稱空間的使用,但需要特別的步驟來利用Dojo工具包內建的測試回合器。Dustin Machi的SitePen部落格提供的指示,可以使Dojo對象甲胄工作。

 

What tools do you use?你使用什麼工具?

 

In Web development, there is a constant state of flux as tools and techniques continue to evolve and be introduced. What tools do you use to implement unit testing or other chores in your everyday work? Share your experiences and recommendations with the Web Developer community.

 

在網站開發領域,隨著工具和技術持續發展和引進而不斷演變。在日常工作中,您用什麼工具來執行單元測試或其他雜務呢?請與Web開發社區分享您的經驗和建議。

 

Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.

 

Tony Patton的專家生涯始於應用程式開發員,獲得Java, VB,Lotus, and XML認證從而增長知識。

 

《Purple Endurer註:1、begin as:以(某職業)開始自己的生涯》

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.