前端测试工具-摩卡(简单了解)

来源:互联网 发布:网络21健康行是传销吗 编辑:程序博客网 时间:2024/06/05 16:52

关于摩卡

mocha 2011年是目前现在最流行的javaScript测试框架。

测试框架就是运行测试的工具通过一系列的方法对javaScript应用进行测试保证代码的质量。

安装项目目录

  • 全局安装 npm install mocha -g
  • npm init
  • npm install mocha –save-dev
  • npm install chai –save-dev(断言库)

mocha声明周期函数

  • before() 测试之前
  • after() 测试之后
  • beforeEach() 每条测试之后
  • afterEach() 每条测试之后
describe("demo",function(){    describe("登录方法",function(){        before(function(){            console.log("测试之前")        })        after(function(){            console.log("测试之后")        })        beforeEach(function(){            console.log("每条测试之前")        })        afterEach(function(){            console.log("每条测试之后")        })        context("情景1的情况下",function(){            it("iphone手机测试",function(){                console.log("通过")            })            it("oppo手机测试",function(){                console.log("通过")            })        })    })})

assert 断言

说白了就是一些布尔表达式,通过启用或者禁用用于辅助验证的这段逻辑或者方法是否是正确的。

  • 断言一段有两种:一种是直接返回布尔类型;一种通过true,或者false来throw

#

  • 我们使用chai断言库
  • 他是提供了三种分割的断言测试(assert should expect)
// assert 测试const chai = require('chai');const assert = chai.assert;describe("进行assert风格断言测试",function(){    it("进行assert测试",function(){        var value = "test"        assert.typeOf(value,"string");        assert.equal(value,"test");        assert.lengthOf(value,4)    })})
// shuold 风格测试const chai = require("chai");const should = chai.should();describe("should风格的测试",function(){    it("进行should测试",function(){        var value = "test";        value.should.be.a("string");        value.should.equal("test");        value.should.not.equal("tests");        value.should.have.length(4);    })})
// expect 断言测试const chai = require('chai');const expect = chai.expect;describe("expect断言测试",function(){    it("expect",function(){        var value = 'test';        expect(value).to.be.a("string");        expect(value).to.equal("test");        expect(value).to.not.equal('tests');        expect(value).to.have.length('4');    })})
0 0
原创粉丝点击