Jasmine编写JavaScript测试用例

发表于:2015-7-02 10:20

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Tom Fan    来源:51Testing软件测试网采编

  1,下载Jasmine 包,主要包括如下三个文件:
  1>,jasmine-html.js,jasmine.css,jasmine.js
  2>,编写测试用例,代码如下:
describe("Examples of Jasmine suite", function() {
//List 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);
});
});
//List 2
describe("Test suite is a function.", function() {
var gVar;
it("Spec is a function.", function() {
gVar = true;
expect(gVar).toBe(true);
});
it("Another spec is a function.", function() {
gVar = false;
expect(gVar).toBe(false);
});
});
//List 3
describe("This is an exmaple suite", function() {
it("contains spec with an expectation", function() {
var num = 10;
expect(num).toEqual(10);
});
});
//List 4
describe("The 'toBe' matcher compares with ===", function() {
it("and has a positive case ", function() {
expect(true).toBe(true);
});
it("and can have a negative case", function() {
expect(false).not.toBe(true);
});
});
//List 5
describe("Included matchers:", function() {
it("The 'toBe' Matcher", function() {
var a = 3.6;
var b = a;
expect(a).toBe(b);
expect(a).not.toBe(null);
});
describe("The 'toEqual' matcher", function() {
it("works for simple literals and variables", function() {
var a = "varA";
expect(a).toEqual("varA");
});
it("Work for objects", function() {
var obj = {
a: 1,
b: 4
};
var obj2 = {
a: 1,
b: 4
};
expect(obj).toEqual(obj2);
});
});
it("The 'toBeDefined' matcher ", function() {
var obj = {
defined: 'defined'
};
expect(obj.defined).toBeDefined();
expect(obj.undefined).not.toBeDefined();
});
});
//List 6
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);
});
});
//List 7
describe("A spec", 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);
});
describe("nested describe", function() {
var tempVar;
beforeEach(function() {
tempVar = 4.6;
});
it("gVar is global scope, tempVar is this describe scope.", function() {
expect(gVar).toEqual(tempVar);
});
});
});
//List 8
xdescribe("An example of xdescribe.", function() {
var gVar;
beforeEach(function() {
gVar = 3.6;
gVar += 1;
});
xit(" and xit", function() {
expect(gVar).toEqual(4.6);
});
});
});
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号