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);
});
});
Matchers
一個一個的測試點們由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();
});
});
//測試一個方法是否抛出異常
Setup和Teardown方法
為了代碼簡潔,減少重複性的工作,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);
});
});
另外describe和it作為方法是可以嵌套的,也就是describe中可以出現子describe和it。
禁用某些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();
}
})();
版權聲明:本文為CSDN部落客「weixin_33989058」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。
原文連結:https://blog.csdn.net/weixin_33989058/article/details/91980327