前端自动化测试karma+mocha+chai

时间:2019-11-21
本文章向大家介绍前端自动化测试karma+mocha+chai,主要包括前端自动化测试karma+mocha+chai使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在多人同时改动代码时, 帮助我们知道是否别的接口或者函数是不是出问题了;

1. Karma

https://karma-runner.github.io/2.0/index.html

Karma为前端自动化测试提供了跨浏览器测试的能力;

2. Mocha

Mocha是前端自动化测试框架;

支持生命周期;不同断言库:chai, nodejs的assert, should.js;同步异步;测试分组;等其他框架具备的能力;

基本语法:

describe('测试1',function(){
    describe('测试1-1',function(){
        it('某个变量的值应该是数字',function(){
            //断言
        })
    });
     describe('测试1-2',function(){
        it('某个数组长度应该不小于10',function(){
            //断言
        })
    });
}) 

生命周期:

describe('hooks', function() {
  before(function() {
    // runs before all tests in this block
  });
  after(function() {
    // runs after all tests in this block
  });
  beforeEach(function() {
    // runs before each test in this block
  });
  afterEach(function() {
    // runs after each test in this block
  });
  // test cases
});

  

3. Chai

Chai是一个断言库合集;

支持语法如下:

expect(bar).to.not.exist;//断言bar不存在
expect(data).to.have.ownProperty('length');//断言data有length属性
expect(name).to.be.a('string');//断言name是一个字符串
assert.equal(value1,value2);//断言value1和value2相等
Tony.should.be.an.instanceof(Person);//断言Tony是Person类的实例

  

原文地址:https://www.cnblogs.com/ljyqd/p/11905437.html