- 前端开发,测试用例的编写时少不了的。为何要编写测试用例呢?
- 这个问题,仁者见仁智者见智吧,
- 从我角度来说,就是在大型项目集成中,能很好的约束代码变动,及时了解到哪些functon not work了,避免出现线上问题。
- 测试用例的编写,大都都基于nodejs开发环境,当然测试用例也能使用web 版本的。
- 本文基于个人电脑开发环境介绍,不足的,请同学自行Google。
jasmine官方地址 替换其中spec文件夹下测试用例即可
node 安装
node install
brew search nodejs
brew install nodejs
jasmine install
npm install jasmine
npm install -g jasmine
// 创建spec 文件夹和jasmine.json
-- 全局安装:
jasmine init
-- 本地安装:
npx jasmine init
Jasmine
宗旨
- 一个it中的所有expect success,整个it才success
- 一个expect失败,整个it测试失败
核心概念
-
beforeAll 所有测试用例调用前,进行全局变量的初始化
-
afterAll 所有测试用例调用后,进行全局变量的重置。
-
beforeEach 每个测试用例执行前,进行特定的变量的初始化。
-
afterEach 每个测试用例执行后,进行特定的变量的重置。
-
describe 定义测试用例it集合
-
it 定义单个测试用例
-
expect 期望,返回Matcher,断言测试用例执行是否成功
-
spyOn 进行service、component function的proxy代理,it运行时,替代对应的业务逻辑,进行黑盒测试,保证代码功能完整性。
-
示例
describe('Hello test', function() {
var hello;
var status = 0;
beforeAll(function(){
hello = new Hello();
});
beforeEach(function() {
status = 1;
});
afterAll(function(){
hello = undefined;
});
afterEach(function(){
status = 0;
});
it("test spyon with no arguments", function() {
spyOn(hello,'sayHello');
hello.sayHello();
//pass
expect(hello.sayHello).toHaveBeenCalledWith();
});
});
其他概念
- fail 把测试用例标记为失败,可传递失败原因
describe("A spec using the fail function", function() {
var foo = function(x, callBack) {
if (x > 60) {
callBack();
}
};
it("should not call the callBack", function() {
foo(70, function() {
fail("should the capacity will be under 60 ");
});
});
});
spyOn
必要的三步曲:
- 1.编写spyOn(obj, methodName)
- 2.调用函数
- 3.进行expect
模型
function Hello(){}
Hello.prototype.sayHello = function(){
console.log("say hello jasmine");
}
Hello.prototype.getName = function(){
console.log("will return xiaoming");
return 'xiaoming'
}
Hello.prototype.getSum = function(a,b){
console.log("will return sum");
return a+b;
}
1.不带参数的函数
describe('Hello test', function() {
var hello;
beforeEach(function() {
hello = new Hello();
});
it("test spyon with no arguments", function() {
spyOn(hello,'sayHello');
hello.sayHello();
//pass
expect(hello.sayHello).toHaveBeenCalledWith();
});
});
2.带参数的函数
it('test spyon with argments',function(){
spyOn(hello, 'getSum');
hello.getSum(5, 10);
// also passes
expect(hello.getSum).toHaveBeenCalledWith(5, 10);
});
3.带有返回值的函数
it("test spyon with no arguments but with return value", function() {
spyOn(hello,'getName')
hello.getName();
//fail
expect(value).toBe('xiaoming');
});
// 此时不能再利用上述的写法,需要添加额外参数
it("test spyon with no arguments but with return value", function() {
spyOn(hello,'getName').and.returnValue('xiaoming');
const value = hello.getName();
//pass
expect(value).toBe('xiaoming');
});
//or
it("test spyon with no arguments but with return value", function() {
// spyOn(hello,'getName').and.returnValue('xiaoming');
spyOn(hello,'getName').and.callFake(function(){
return 'xiaoming';
});
const value = hello.getName();
//pass
expect(value).toBe('xiaoming');
});
函数真实调用
1.不带参数
it("test spyon callthrough with no arguments", function() {
spyOn(hello,'sayHello').and.callThrough();
hello.sayHello();
//pass,且函数触发console
expect(hello.sayHello).toHaveBeenCalled();
});
2.带参数
it('test spyon callthrought with argments',function(){
spyOn(hello, 'getSum').and.callThrough();
const value = hello.getSum(5, 10);
// also passes,触发console
expect(hello.getSum).toHaveBeenCalledWith(5, 10);
expect(value).toBe(15);
});
xdescribe
跳过describe中所有的测试用例,不会被执行
xdescribe('Hello test', function() {
var hello;
beforeEach(function() {
hello = new Hello();
});
it("test spyon with no arguments", function() {
spyOn(hello,'sayHello');
hello.sayHello();
//pass
expect(hello.sayHello).toHaveBeenCalledWith();
});
});
xit - 跳过测试用例,不会被执行
xit("test spyon callthrough with no arguments", function() {
spyOn(hello,'sayHello').and.callThrough();
hello.sayHello();
//pass
expect(hello.sayHello).toHaveBeenCalled();
});
fdescribe
重点关注的测试用例集合,其余的不会被执行。相当于xdescribe取反。
fit
重点关注的测试用例
fdescribe('test with callThrough',function(){
var hello;
beforeEach(function() {
hello = new Hello();
});
fit("test spyon callthrough with no arguments", function() {
spyOn(hello,'sayHello').and.callThrough();
hello.sayHello();
//pass
expect(hello.sayHello).toHaveBeenCalled();
});
it('test spyon callthrought with argments',function(){
spyOn(hello, 'getSum').and.callThrough();
const value = hello.getSum(5, 10);
// also passes
expect(hello.getSum).toHaveBeenCalledWith(5, 10);
expect(value).toBe(15);
});
});
pending
处于暂停的测试用例,忽略测试执行结果。