JS 代码测试工具:Jasmine

下载地址:https://github.com/jasmine/jasmine#installation

Jasmine不依赖其它任何 JavaScript 框架,也不需要 DOM。它的语法简洁、明确,写测试非常容易。
Jasmine支持多种浏览器,包括Safari, Chrome, Firefox, PhantomJS, new Internet Explorer,同时也支持node。

可以在html中直接编写代码进行测试,首先将相关文件引入:

<link rel="shortcut icon" type="image/png" href="jasmine/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href="jasmine/jasmine.css">
<script type="text/javascript" src="jasmine/jasmine.js"></script>
<script type="text/javascript" src="jasmine/jasmine-html.js"></script>
<script type="text/javascript" src="jasmine/boot.js"></script>

引入了jasmine后,就可以在js文件中写测试用例了。测试结果可以直接显示在html页面上。

//测试用例1
describe("This is an exmaple suite", function() {
  it("contains spec with an expectation", function() {
    expect(true).toBe(true);
    expect(false).toBe(false);
    expect(false).not.toBe(true);
  });
});

输出结果:

2.5.2
Options *(下面这几行是可选框,可以供使用者根据需要选择)*
    raise exceptions
    stop spec on expectation failure
    run tests in random order

finished in 0.008s1 spec, 0 failures
    This is an exmaple suite
        contains spec with an expectation

测试用例2:

describe("Test suite is a function.", function() {
    var gVar;

    it("Spec is a function.", function() {
      gVar = false;
      expect(gVar).toBe(true);//注意这里包含了一个错误
    });

    it("Another spec is a function.", function() {
      gVar = false;
      expect(gVar).toBe(false);
    });
});

输出结果:

2.5.2
Options
    raise exceptions
    stop spec on expectation failure
    run tests in random order

finished in 0.009s2 specs, 1 failureSpec List | FailuresSpec List | Failures
Test suite is a function. Spec is a function.
Expected false to be true.
stack@file: ...
(这里会详细列出发生错误的位置信息)
Test suite is a function.
    Spec is a function.
    Another spec is a function.

jasmine中还包含有很多可供测试者使用的测试函数,例如:

toBe()
toNotBe()
toBeDefined()
toBeUndefined()
toBeNull()
toBeTruthy()
toBeFalsy()
toBeLessThan()
toBeGreaterThan()
toEqual()
toNotEqual()
toContain()
toBeCloseTo()
toHaveBeenCalled()
toHaveBeenCalledWith()
toMatch()
toNotMatch()
toThrow()

另外, Jasmine 提供了全局的 beforeEach 和 afterEach 方法。beforeEach 方法在 describe 中的每个 it 函数执行之前运行,afterEach 在每个 it 调用后运行。基于此,我们可以在beforeEach方法中对变量进行初始化,在afterEach将变量重置。
栗子:

describe("An example of setup and teardown)", function() {
  var gVar;

  beforeEach(function() {
    gVar = 3.6;
    gVar += 1;
  });

  afterEach(function() {
    gVar = 0;
  });

  it("after setup, gVar has new value.", function() {
    expect(gVar).toEqual(4.6);
  });

  it("A spec contains 2 expectations.", function() {
    gVar = 0;
    expect(gVar).toEqual(0);
    expect(true).toEqual(true);
  });
});

更多方法和细节,可以参见:https://jasmine.github.io

下面的链接中收录了常用的js测试用具:
http://www.oschina.net/translate/12-must-have-user-testing-tools

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值