客户端JavaScript代码单元测试方案

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:以(某职业)开始自己的生涯》

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值