`
caobihole
  • 浏览: 949758 次
文章分类
社区版块
存档分类
最新评论

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

分享到:
评论

相关推荐

    Aspect-js:适用于SPA的Fullstack JavaScript UI框架,包括。 渲染,mvc,模型,表达语言,数据源,虚拟路径,单元测试等

    受JSF(Java Server Faces)关于功能以及易于集成到标记方面的良好经验的影响,出现了类似的客户端全栈解决方案。 Seanox Aspect-js专注于实现单页应用程序(SPA)的简约方法。 该框架采用了HTML的声明性方法,并...

    cobe-test:Cobe Javascript 测试

    Cobe JavaScript 测试 Fork 这个存储库并提交一个链接和你的求职信和简历! 这些作业没有单一的“正确”答案,但优雅的解决方案会获得加分。 这个练习的目的是证明你可以学习未知的代码库,让你展示你的 JS 技能。 ...

    JSValidation-----强大而灵活的表单客户端验证框架

    但是在大多数情况下,出于种种原因,开发者宁愿(或者没办法)重新编写另一个页面的JavaScript代码。 <br>JSValidation致力于改善这一过程。它将常见的校验(目前支持13种)封装起来,并创造性的采用xml来存贮...

    JAVA上百实例源码以及开源项目源代码

    5个目标文件,演示Address EJB的实现 ,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口...

    js-file-download:此代码是使用JavaScript在网页中打开下载文件弹出窗口的最佳解决方案

    此代码是使用JavaScript在网页中打开下载文件弹出窗口的最佳解决方案。 如果您需要使用javascript将文件下载到客户端浏览器,则只需要使用filePath网址作为输入参数来调用downloadFile(filePath)函数,它将...

    Backbone.js应用程序开发

    此外,backbone和grunt-bbb、jquery mobile等开发工具的配合使用,以及jasmine、qunit和sinonjs等测试解决方案。, 本书的作者是知名的javascript专家、谷歌chrome团队的工程师addy osmani。本书适合于javascript...

    BACKBONE.JS应用程序开发

    此外,backbone和grunt-bbb、jquery mobile等开发工具的配合使用,以及jasmine、qunit和sinonjs等测试解决方案。, 本书的作者是知名的javascript专家、谷歌chrome团队的工程师addy osmani。本书适合于javascript...

    JAVA上百实例源码以及开源项目

    5个目标文件,演示Address EJB的实现,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口...

    Backbone.js应用程序开发 中文清晰完整版pdf

    此外,backbone和grunt-bbb、jquery mobile等开发工具的配合使用,以及jasmine、qunit和sinonjs等测试解决方案。 本书的作者是知名的javascript专家、谷歌chrome团队的工程师addy osmani。本书适合于javascript...

    【卷一/共两卷】AJAX实战pdf高清版90M

    10.2.2 测试服务器端代码. 10.3 客户端框架 10.3.1 HTML 10.3.2 JavaScript 10.3.3 访问服务器 10.4 添加功能:包含不同查询的多个元素 10.5 重构 10.5.1 第一天:开发TextSuggest组件的游戏计划 10.5.2 第2天:创建...

    编写Js代码要注意的几条规则

    检查客户端支持对象的能力(渐进式)而不是检查其客户端,测试要使用的对象。 3.访问既有HTML中的内容而不是通过Js添加HTML(行为层与结构层分离) 4.不要使用专有DOM对象(例如IE的document.all) 5.将脚本放进一个...

    sample-hackernews-client-react-app:获取并显示 HackerNews 的示例React应用程序

    示例 HackerNews 客户端基于 React 和 Redux 构建,并带有... 如果您的单元测试足够安全,那么在代码中进行大的重构或优化时就会更容易呼吸和测试。 还有更多原因,以上是我自己的发现。 随意分享你的。我正在使用 d

    迷宫挑战

    我想在客户端生成迷宫并测试解决方案,以避免服务器上的负载。 当然,通过放弃迷宫生成和解决方案检查代码,您几乎可以放弃解决迷宫的算法:) WASM可以帮助解决此问题,因为解决这种愚蠢的迷宫比从反编译的WASM中...

    next-on-drupal:基于Drupal后端应用程序的NextJS样板

    接下来在Drupal上 该项目是一个样板,旨在促进基于通过公开的后端的项目的。 在您可以找到有关许多概念,解决方案以及...) 代码/项目质量 捆绑分析仪单元测试 配置笑话 添加覆盖率阈值 测试样板代码 端到端测试 设置柏

    Asp.net-OAuth2-e-Owin-com-Javascript

    带有OAuth2和Owin的ASP.net WebApi 要使用,只需下载项目并在visualstudio.net中打开它(最好是2017年) 示范 然后按照以下步骤同时运行项目: ... 阅读该项目的index.HTML的Javascript代码,它发出一个简单的请求。

    relayr-node-applicant-test:测试申请

    解决方案 其思想是减少发送方和接收方之间的数据传输量,我们可以... 您应该为所编写的代码提供单元测试。 测试工具的选择取决于您。 要求 该系统模拟一个客户端,它将传感器读数发送到远程服务器。 我们想让您想象一

    d3trends:带有RequireJS的d3js用于趋势图

    干净,可维护,易于阅读且经过单元测试的代码 良好的建筑规范 干净HTML和CSS标记 强烈考虑 我们鼓励使用D3JS库 避免使用外部框架,例如jQuery,AngularJS或React JS。 以最适合您的需求的格式加载数据模型。 避免...

    Bot-Blocker:基于现有开放源代码工具和项目的浏览器指纹识别工具,用于阻止机器人活动。 在测试的早期阶段

    击败此软件的最新版本: : (注意:尚未测试所有方案)。概述和致谢我决定开始这个项目是因为注意到大多数与此类似的开源项目已经停产或被专有。 该工具的主要目的是使Web开发人员能够立即识别各种条纹的非人类...

Global site tag (gtag.js) - Google Analytics