Pure JS (6.2):结合 Rhino 和 Junit GUI 测试 JS

[size=large][align=center][b]Pure JS (6.2):结合 Rhino 和 Junit GUI 测试 JS[/b][/align][/size]
  最近尝试了直接用 Rhino 启动 Junit ,刚开始就遇到了一些问题。因为 Java 注解在 JavaScript 中并不能起到什么作用,所以我选择了 Junit 3,并使用了 Junit 3 自带的swing GUI。最大的问题是 Junit 是通过 Class 名称加载测试类进行测试的。而 Rhino 的 Class 是动态生成的,名称也比较怪(比如 adapter1 之类),并不适合显示在界面上,因此我修改了 Juni 3 的源代码(见附件)。可以用“Pei Xiaoxing”作为关键字在源代码中搜索修改过的地方。

  所有的测试案例都放在 PureJS 工程中的 scripts/test 目录下,可以用上一篇文章中介绍的方法启动测试:
  1. scripts/test/sameple 目录下以“run”开头的 js 文件,加载并运行范例;
  2. scripts/test/app/testAll.js, 测试 scripts/app 目录的 js 文件;
  3. scripts/test/both/views.js,测试 webapp/both/views.js;
  4. scripts/test/lib 目录下的 files.js、db.js、render.js,分别测试 scripts/lib 下对应的 js 文件;
  5. script/test/servlets/servlets.js,测试 scripts/servlets 目录的 js 文件。
  需要注意的是数据库操作的部分并未进行 mock,因此运行数据库相关的测试案例是需要先通过命令行启动 mongod。

  下面以 scripts/test/sameple 目录下的文件为例进行介绍。

[size=medium][align=center][b]被测试的 JS 文件[/b][/align][/size][b]  add.js[/b]
[code]
add = {
two: function(a, b) { return a + b; },
three: function(a, b, c) { return a + b + c; }
}
[/code]
[size=medium][align=center][b]TestCase 的使用[/b][/align][/size]  
  【编写测试案例】

  编写测试案例的一般步骤如下:
  1. 加载被测试的文件
  2. 指定测试案例的名称
  3. 编写测试函数

  [b]testAdd.js[/b]
[code]
load("scripts/test/sample/add.js");

testAdd = {
name: function() {
return "Test Add";
},
testAddTwo: function() {
Assert.assertEquals(3, add.two(1, 2), 0);
},
testAddThree: function() {
Assert.assertEquals(6, add.three(1, 2, 3), 0);
}
};
[/code]
  需要注意的是,JavaScript 中的数值实际上都是 float 类型的,因此使用 Assert.assertEuals 时需要指定误差范围(第三个参数)。但这里验证的是整数,因此误差为 0 。

  【加载测试案例】

  加载测试案例的一般步骤如下:
  1. 加载 scripts/test/test.js,它定义了 test 函数
  2. 加载包含测试案例的 js 文件
  3. 调用 test 函数执行测试案例

  [b]runTestAdd.js[/b]
[code]
load("scripts/test/test.js");
load("scripts/test/sample/testAdd.js");

test(testAdd);
[/code]

  【执行测试案例】
  参照上一篇文章创建 Run Congiguration,Arguments 设置为 scripts/test/sample/runTestAdd.js (如图)。

[align=center][img]http://dl.iteye.com/upload/picture/pic/94884/3e49136c-90b5-3cca-8972-8440b6c484bc.png[/img][/align]

  点击 Run 按钮执行(第一次执行后可以在快捷执行按钮的下拉菜单中看到)。

[align=center][img]http://dl.iteye.com/upload/picture/pic/94892/b1b0bdd1-e5bc-3887-9ff3-67e9f923c2e8.png[/img][/align]

  执行效果如下:

[align=center][img]http://dl.iteye.com/upload/picture/pic/94886/8337eccd-8bda-3509-a4a3-8899bdc2d53a.png[/img][/align]

[size=medium][align=center][b]TestSuite 的使用[/b][/align][/size]
  【编写测试案例案例集】

  编写测试案例的一般步骤如下:
  1. 加载要包含在测试案例集中的测试案例
  2. 指定测试案例集的名称
  3. 编写返回测试案例数组的 suite 函数

  [b]testSuite.js[/b]
[code]
load("scripts/test/sample/testAdd.js");
load("scripts/test/sample/testMinus.js");

testSuite = {
name: function() {
return "Test Suite";
},
suite: function() {
return [ testAdd, testMinus ];
}
};
[/code]
  【加载测试案例集】

  加载测试案例的一般步骤如下:
  1. 加载 scripts/test/test.js,它定义了 test 函数
  2. 加载包含测试案例的 js 文件
  3. 调用 test 函数执行测试案例

  [b]runTestSuite.js[/b]
[code]
load("scripts/test/test.js");
load("scripts/test/sample/testSuite.js");

test(testSuite);
[/code]
  【执行测试案例集】
  Arguments 设置为 scripts/test/sample/runTestSuite.js ,执行效果如下:

[align=center][img]http://dl.iteye.com/upload/picture/pic/94888/8e97d0f8-b6d6-3741-b318-4d6030a53764.png[/img][/align]
  [size=medium][align=center][b]TestSuite 的嵌套[/b][/align][/size]
  【编写嵌套测试案例集】

  测试案例集是可嵌套的,即一个测试案例集可以被包含在另一个测试案例集中。这在需要同时执行多个测试案例集时很有用。
  编写嵌套测试案例的一般步骤如下:
  1. 加载要包含在嵌套测试案例集中的测试案例集或测试案例
  2. 指定嵌套测试案例集的名称
  3. 编写返回测试案例集和测试案例数组的 suite 函数

  [b]nestedSuite.js[/b]
[code]
load("scripts/test/test.js");
load("scripts/test/sample/testSuite.js");

nestedSuite = {
name: function() {
return "Nested Suite";
},
suite: function() {
return [ testAdd, testSuite ];
}
};
[/code]
  这个嵌套案例集包含了一个普通的测试案例和一个测试案例集。

  【加载嵌套测试案例集】

  加载嵌套测试案例的一般步骤如下:
  1. 加载 scripts/test/test.js,它定义了 test 函数
  2. 加载包含测试案例集的 js 文件
  3. 调用 test 函数执行嵌套测试案例

  [b]runNestedSuite.js[/b]
[code]
load("scripts/test/sample/nestedSuite.js");

test(nestedSuite);
[/code]
  【执行嵌套测试案例集】
  Arguments 设置为 scripts/test/sample/runNestedSuite.js ,执行效果如下:

[align=center][img]http://dl.iteye.com/upload/picture/pic/94890/f4b7f218-4711-3b38-94d2-783ef76cffea.png[/img][/align]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值