jasmine測試框架簡介
jasmine是一種javascript測試框架,它既可以在html文件中運行,也可以和jsTestDriver整合,在jsTestDriver中運行。
jasmine的簡單語法
一個基本的jasmine測試用例如下:
describe("A suite", function() {
it("contains spec with an expectation", function() {
expect(true).toBe(true);
});
});
describe方法
describe方法標志著一個測試集(test suite)的開始,這個方法有兩個參數,一個字符串String,一個方法function;字符串用來描述我們這個test suite,function里的東西就是測試代碼,它們就是suite。
it方法
jasmine中用方法it來開始specs(我理解成測試點,一個測試suite里可以有很多spec)。it方法和describe方法類似, 同樣有兩個參數,一個String,一個function;String用來描述測試點(spec),function是具體的測試代碼。一個測試點 (spec)可以包含多個expections(其實是斷言的意思)。
expectations
斷言可以返回為true或者false。全部的斷言返回true這個測試點就通過,一個或者多個斷言返回false這個測試點就不通過。
describe和it都是方法,我們可以自定義一些變量,在describe中定義的變量,在it方法中可以直接使用。
describe("A suite is just a function", function() {
var a;
it("and so is a spec", function() {
a = true;
expect(a).toBe(true);
});
}); </pre> <h4>Matchers</h4>
一個一個的測試點們由expect開頭,后面跟著一個我們需要測試的變量,如上面的a,然后跟著一個Matcher方法(我理解成校驗規
則),Matcher方法帶著一個期望值,如上面的true。Matchers方法返回true或者false,它決定著測試點(spec)是否通過。所
有的Matchers方法都能在mathcer前面加上not來進行否定斷言,如`expect(a).not.toBe(true);
jasmine中有很多Matchers方法供我們使用,當然我們也可以定義自己的Matchers方法。
describe("Included matchers:", function() {
it("The 'toBe' matcher compares with ===", function() {
var a = 12;
var b = a;
expect(a).toBe(b);
expect(a).not.toBe(null);
});
//上面的例子,比較a、b是否相等;驗證a是否不是空。
it("should work for objects", function() {
var foo = {
a: 12,
b: 34
};
var bar = {
a: 12,
b: 34
};
expect(foo).toEqual(bar);
});
//上面的例子比較了兩個對象是否相等
});
it("The 'toMatch' matcher is for regular expressions", function() {
var message = 'foo bar baz';
expect(message).toMatch(/bar/);
expect(message).toMatch('bar');
expect(message).not.toMatch(/quux/);
});
//也可以使用正則表達式
it("The 'toBeDefined' matcher compares against `undefined`", function() {
var a = {
foo: 'foo'
};
expect(a.foo).toBeDefined();
expect(a.bar).not.toBeDefined();
});
//驗證變量是否被定義
it("The 'toBeNull' matcher compares against null", function() {
var a = null;
var foo = 'foo';
expect(null).toBeNull();
expect(a).toBeNull();
expect(foo).not.toBeNull();
});
//驗證是否為空
it("The 'toBeTruthy' matcher is for boolean casting testing", function() {
var a, foo = 'foo';
expect(foo).toBeTruthy();
expect(a).not.toBeTruthy();
});
it("The 'toBeFalsy' matcher is for boolean casting testing", function() {
var a, foo = 'foo';
expect(a).toBeFalsy();
expect(foo).not.toBeFalsy();
});
//變量是否能夠轉化成boolean變量? 不太確定
it("The 'toContain' matcher is for finding an item in an Array", function() {
var a = ['foo', 'bar', 'baz'];
expect(a).toContain('bar');
expect(a).not.toContain('quux');
});
//是否包含
it("The 'toBeLessThan' matcher is for mathematical comparisons", function() {
var pi = 3.1415926, e = 2.78;
expect(e).toBeLessThan(pi);
expect(pi).not.toBeLessThan(e);
});
it("The 'toBeGreaterThan' is for mathematical comparisons", function() {
var pi = 3.1415926, e = 2.78;
expect(pi).toBeGreaterThan(e);
expect(e).not.toBeGreaterThan(pi);
});
//數學大小的比較
it("The 'toBeCloseTo' matcher is for precision math comparison", function() {
var pi = 3.1415926, e = 2.78;
expect(pi).not.toBeCloseTo(e, 2);
expect(pi).toBeCloseTo(e, 0);
});
//兩個數值是否接近,這里接近的意思是將pi和e保留一定小數位數后,是否相等。(一定小數位數:默認為2,也可以手動指定)
it("The 'toThrow' matcher is for testing if a function throws an exception", function() {
var foo = function() {
return 1 + 2;
};
var bar = function() {
return a + 1;
};
expect(foo).not.toThrow();
expect(bar).toThrow();
});
});
//測試一個方法是否拋出異常 </pre> <h4>Setup和Teardown方法</h4>
為了代碼簡潔,減少重復性的工作,jasmine提供beforeEach和afterEach方法。beforeEach會在每個spec之前執行,after會在每個spec之后執行,類似于selenium中的beforeMethod和afterMethod方法。
describe("A spec (with setup and tear-down)", function() {
var foo;
beforeEach(function() {
foo = 1;
});
afterEach(function() {
foo = 0;
});
it("is just a function, so it can contain any code", function() {
expect(foo).toEqual(1);
});
it("can have more than one expectation", function() {
expect(foo).toEqual(1);
expect(true).toEqual(true);
});
}); </pre> <p>另外describe和it作為方法是可以嵌套的,也就是describe中可以出現子describe和it。</p>
禁用某些spec和suites
在測試中,我們可能需要禁用一些suites和spec,方法是使用xdescribe和xit方法,這些測試的方法會被忽略,不計入統計結果。
The Runner and Reporter
Jasmine是用javascript實現的,所以它也必須在javascript的環境中運行,最簡單的環境也就是一個web頁面。所有的spec都可以在這個頁面中運行,這個頁面就叫做Runner。
Jasmine通過下面的js代碼來展現spec運行結果:
var htmlReporter = new jasmine.HtmlReporter(); //創建一個HTMLReporter
jasmineEnv.addReporter(htmlReporter);
jasmineEnv.specFilter = function(spec) { //一個過濾器,允許我們點擊單個的suites,單獨運行
return htmlReporter.specFilter(spec);
};
var currentWindowOnload = window.onload; //頁面加載完畢后,執行所有的test。
window.onload = function() {
if (currentWindowOnload) {
currentWindowOnload();
}
document.querySelector('.version').innerHTML = jasmineEnv.versionString();
execJasmine();
};
function execJasmine() {
jasmineEnv.execute();
}
})(); </pre><strong>來自</strong><strong>:</strong> <a href="/misc/goto?guid=4959554440722471567">jasmine測試框架簡介</a>