一個(gè)測(cè)試?yán)踝?/h6>
//建立個(gè)describe塊
describe('JavaScript addition operator',function(){
let arr=[1,2,3,4,5];
//建立it塊
it('test function sumArray',function(){
//測(cè)試函數(shù)sumArray返回結(jié)果是否為15
expect(sumArray(arr)).toEqual(15);
});
});
//建立個(gè)describe塊
describe('JavaScript addition operator',function(){
let arr=[1,2,3,4,5];
//建立it塊
it('test function sumArray',function(){
//測(cè)試函數(shù)sumArray返回結(jié)果是否為15
expect(sumArray(arr)).toEqual(15);
});
});
Suite表示一個(gè)測(cè)試集,以函數(shù)describe(string, function)封裝若债,它包含2個(gè)參數(shù):
string:測(cè)試組名稱符相,
function:測(cè)試組函數(shù)成肘。
一個(gè)Suite(describe)包含多個(gè)Specs(it)帘靡,一個(gè)Specs(it)包含多個(gè)斷言(expect)
常用的操作函數(shù)
Setup在每個(gè)測(cè)試用例Spec執(zhí)行之前做一些初始化操作淆攻,Teardown在每個(gè)Sepc執(zhí)行完之后做一些清理操作
- beforeEach():在describe函數(shù)中每個(gè)Spec執(zhí)行之前執(zhí)行
- afterEach(): 在describe函數(shù)中每個(gè)Spec數(shù)執(zhí)行之后執(zhí)行
- beforeAll():在describe函數(shù)中所有的Specs執(zhí)行之前執(zhí)行行嗤,但只執(zhí)行一次已日,在Sepc之間并不會(huì)被執(zhí)行
- afterAll(): 在describe函數(shù)中所有的Specs執(zhí)行之后執(zhí)行,但只執(zhí)行一次栅屏,在Sepc之間并不會(huì)被執(zhí)行
常用的Matchers(expect(斷言)的比較操作)
一般用來判斷返回的結(jié)果是否和預(yù)期結(jié)果一致飘千,返回 True 或 False
expect(sumArray(arr)).toEqual(15);
- toEqual():相當(dāng)于==
- toNotEqual() 檢查變量或?qū)傩允欠癫坏扔陬A(yù)期值
- toBe():相當(dāng)于===
- toBeDefined():檢查變量或?qū)傩允欠褚崖暶髑屹x值
- toBeNull():檢查變量或?qū)傩允欠袷莕ull
- toBeLessThan():數(shù)值比較,小于
- toBeGreaterThan():數(shù)值比較栈雳,大于
- toContain():數(shù)組中是否包含元素(值)(只能用于數(shù)組护奈,不能用于對(duì)象)
- toHaveBeenCalledWith() 判斷調(diào)用函數(shù)時(shí)的參數(shù)
- toHaveBeenCalled() : 函數(shù)是否被調(diào)用
禁用測(cè)試
Suites可以被Disabled。在describe函數(shù)名之前添加x即可將Suite禁用
被Disabled的Suites在執(zhí)行中會(huì)被跳過哥纫,該Suite的結(jié)果也不會(huì)顯示在結(jié)果集中
xdescribe('JavaScript addition operator',function(){
let arr=[1,2,3,4,5];
//建立it塊
it('test function sumArray',function(){
//測(cè)試函數(shù)sumArray返回結(jié)果是否為15
expect(sumArray(arr)).toEqual(15);
});
});
掛起Specs
被Pending的Spec不會(huì)被執(zhí)行霉旗,但是Spec的名字會(huì)在結(jié)果集中顯示,只是標(biāo)記為Pending
掛起Specs的方法:
如果在Spec函數(shù)it的函數(shù)名之前添加x(xit)蛀骇,那么該Spec就會(huì)被標(biāo)記為Pending
一個(gè)沒有定義函數(shù)體的Sepc也會(huì)在結(jié)果集中被標(biāo)記為Pending
如果在Spec的函數(shù)體中調(diào)用pending()函數(shù)厌秒,那么該Spec也會(huì)被標(biāo)記為Pending。pending()函數(shù)接受一個(gè)字符串參數(shù)擅憔,該參數(shù)會(huì)在結(jié)果集中顯示在PENDING WITH MESSAGE:之后鸵闪,作為為何被Pending的原因
describe("unit test sumPrice", function () {
let inputs;
beforeEach(function () {
inputs = [
{
barcode: 'ITEM000001',
name: '雪碧',
unit: '瓶',
price: 3.00,
count: 5
}
];
});
xit("should return sumPrice", function () { //pending
const sumed = [
{
barcode: 'ITEM000001',
name: '雪碧',
unit: '瓶',
price: 3.00,
count: 5,
sum: 12,
save:3
}
];
expect(sumPrice(inputs)).toEqual(sumed);
});
it("can be declared with 'it' but without a function");
it("should return sumPrice", function () {
const sumed = [
{
barcode: 'ITEM000001',
name: '雪碧',
unit: '瓶',
price: 3.00,
count: 5,
sum: 12,
save:3
}
];
expect(sumPrice(inputs)).toEqual(sumed);
pending('this is why it is pending'); //調(diào)用pending()函數(shù)
});
});
執(zhí)行結(jié)果: