Understand JavaScript Testing
Understand JavaScript Testing
Understand JavaScript Testing
com
2010-10-14
UNDERSTANDING JAVASCRIPT
TESTING
Why?
Cross-browser issues.
跨浏览器问题 ;
The possibility for causing an
unforeseen problem is simply too
great.
不可预见的问题的存在性很大 ;
How?
Test strategy
- End-to-end test:
- Big, Powerful, Convincing;
- Slow, In-exact, High-maintenance;
- Unit tests
- Small, Quick, Focused, Resilient;
- Limited;
- Component tests
- the balance between many fast test
and a few slow tests;
Test Methods
- 测试脚本 + 模拟环境 ;
- 测试脚本 + 驱动真实浏览器 ;
- 直接在浏览器中 Unit test;
- 模拟环境下进行 Unit test;
Unit Testing
Assertions
Mock Objects
Asynchronous Tests
- wait;
- resume;
Test Suites
Test Runner
Test Reporting
var testCase = new Y.Test.Case({
testSpecialValues : function () {
Y.Assert.isFalse(false); //passes
Y.Assert.isTrue(true); //passes
Y.Assert.isNaN(NaN); //passes
Y.Assert.isNaN(5 / "5"); //passes
Y.Assert.isNotNaN(5); //passes
Y.Assert.isNull(null); //passes
Y.Assert.isNotNull(undefined); //passes
Y.Assert.isUndefined(undefined); //passes
Y.Assert.isNotUndefined(null); //passes
}
});
Behavior Testing
- code is specification;
- describe 即是 TestCase, 也是 TestSuite;
- ignore 更简单,加上 x 即可 ;
- Matchers 可自定义,可覆盖,可添加 ;
- toThrow 比 YUI Test 更易用 ;
- expect 本身就是一句描述,无需注释 ;
- Spies
describe('Calculator', function () { var testCase = new Y.Test.Case({
var counter = 0
name: "TestCase Name",
Why TDD?
- Makes you think about required behavior;
- Reduces speculative code;
- Provides documentation;
- Improves quality;
BDD
BDD 的重点是通过与利益相关者的讨论取得对预期的软件行为的清醒认识。
它通过用自然语言书写非程序员可读的测试用例扩展了测试驱动开发方法。
行为驱动开发人员使用混合了领域中统一的语言的母语语言来描述他们的代码的目的。
这让开发着得以把精力集中在代码应该怎么写,而不是技术细节上,
而且也最大程度的减少了将代码编写者的技术语言与商业客户、用户、利益相关者、
项目管理者等的领域语言之间来回翻译的代价。
Jasmine 实战
Specs:
说明 , 使用 it(description, fn) 来描述 ;
describe('Calculator', function () {
it('can add a number', function () {
...
});
toBeLessThan: function(expected) {
return this.actual < expected;
};
beforeEach(function() {
this.addMatchers({
toBeVisible: function() { return
this.actual.isVisible(); }
});
});
Spies
permit many spying, mocking, and faking behaviors.
用于模拟传参 , 回调函数 , 异步请求 / 行为监测
expect(obj.method).toHaveBeenCalledWith('foo argument');
- Browser launching
- WebDriver;
- Waitr;
- JsTestDriver;
- Selenium RC;
- Server-Side
- Ignore the browser! Simulate it on the server-side;
- Almost always uses Java + Rhino to construct a browser;
- Some frameworks
- Crosscheck: Pure Java, even simulates browser bugs;
- Env.js: Pure JavaScript, focuses on standards support;
- Blueridge: Env.js + Screw.Unit + Rhino;
- Distributed
- Selenium Grid
- Push Selenium tests out to many machines(that you manage),
simultaneously;
- Collect and store the results;
- TestSwarm
- Push tests to a distributed swarm of clients;
- results viewable on the server;
- testswarm.com;
The Scaling Problem
- All need to be run for every commit, patch, and
plugin;
- JavaScript testing doesn't scale well;
Distributed Testing
- Hub server;
- Clients connect and help run test;
- A simple Javascript client that can be run in all
browsers, including mobile browsers;
- TestSwarm;
JSTestDriver
- 服务端 / 客户端 ,
- test runner 捕获浏览器 , 通过命令通知服务器进行测试 .
然后每个被捕获的浏览器运行 tests, 并将结果返回 ;
- 优点 :
- 运行测试不需要手工跟浏览器进行交互 ;
- 可在多台机器上运行 , 包括移动设备 , 允许任意复杂的测试 ;
- 测试非常快速 , 因为不需要操作 DOM, 且多个浏览器中是同时进行 ;
- 现已支持异步请求测试 ;
- 缺点 :
- JavaScript required to run tests is
slightly more advanced, and may cause a
problem in old browsers;
使用 JSTestDriver
目录结构 :
JSTestDriver
- jsTestDriver.conf # 配置文件
- JsTestDriver-1.2.2.jar # 核心程序 , 包含客户端 / 服务器
- src/ # 待测试 js 源码
- src-test/ # js 测试脚本
配置 :
server: http://localhost:9876
load:
- src/*.js
- src-test/*.js
服务器 : java -jar -jar
D:\workspace\Test>java JsTestDriver-1.2.2.jar --
JsTestDriver-1.2.2.jar --tests all --verbose
[PASSED] cookie get.test that it should return the cookie value for the
port
given na 9876
me
浏览器捕获 : http://localhost:9876/capture
[PASSED] cookie get.test that it should return undefined for non-
existing name
运行测试
[PASSED]:cookie
java -jarthatJsTestDriver-1.2.2.jar
set.test it should set a cookie with a given name --
and value
tests all
[PASSED] cookie remove.test that it should remove a cookie from the
machine
[PASSED] json stringify.test that it should convert an arbitrary value to a
JSON
string representation
[PASSED] json parse.test that it should parse a JSON string to the native
JavaSc
ript representation
Total 6 tests (Passed: 6; Fails: 0; Errors: 0) (0.00 ms)
Firefox 3.6.10 Windows: Run 6 tests (Passed: 6; Fails: 0; Errors 0) (0.00
结合 jasmine
更改配置为 :
server: http://localhost:9876
load:
- ../github/new/kissy/tests/jasmine/jasmine.js
<-----
- ../github/jasmine-jstd-adapter/src/JasmineAdapter.js
<-----
- ../github/new/kissy/src/kissy/*.js
- ../github/new/kissy/src/cookie/cookie.js
- ../github/new/kissy/src/cookie/tests/cookie.js
IDE 中使用
- 自动报告的生成
- 众包测试
- 全网测试
Thanks for your
attention!