javascript测试学习笔记

前端为何须要编写测试用例?

  • 正确性。javascript缺乏类型检查,编译期间没法定位到错误 (typescript无此问题)
  • 自动化。编写一次用例,屡次执行。哪怕代码重构也能在必定程度上保证质量。
  • 解释性。其余开发人员阅读测试用例,能更容易明白该api的做用。

项目的实际状况

  • 业务代码变更频繁。只能简单的能够对一些工具函数等作单元测试
  • 对后端返回的数据写测试。可是相对来讲Swagger更方便。
  • 项目开发时间紧张。只能对一些公共组件写测试。

怎么作测试?

应该先了解最核心的断言模块/库,而后结合现有的测试框架和管理工具进行开发测试用例。javascript

断言

所谓"断言",就是判断源码的实际执行结果与预期结果是否一致,若是不一致就抛出一个错误。 经常使用的断言库有assertshould.jschai.jshtml

Assert(Node 10.13.0)

assert.deepStrictEqual(actual,expected[,message])前端

对比actual和expected参数是否深度相等。便可枚举属性也会按照规则比较。java

const assert = require('assert');

let a = {
  b: 1
}

let b = {
  b: 2
}

assert.deepStrictEqual(a,b,'budui')
复制代码

最关键的是最后一行代码,当比较的两个对象不深度相等时,则Assert抛出错误node

AssertionError [ERR_ASSERTION]: 对象不相等 at Object.git

Should.js

用法上其实和assert差很少。api相对较多。具体参考官网。github

测试框架

实际业务测试中,优秀的测试框架必不可少,有Mocha、Jasmine、Tape、jest等ajax

Mocha

  • 一个功能丰富的前端测试框架
  • 自己没有断言库,须要自行导入断言库
  • 安装: npm install mocha -g
  • 执行: mocha (默认运行test子目录里面的测试脚本)

基本用法

describe('addTest', function () {
  it('0.1加上0.2等于0.3', function () {
    // 使用should.js断言
    add(0.1, 0.2).should.equal(0.3)
  })
})
复制代码

Mocha 测试用例如上,主要包含下面几部分:typescript

describe 定义的测试套件(test suite),第一个参数是套件名称,第二个参数是一个函数npm

it 定义的测试用例(test case),第一个参数是用例名称,第二个参数是一个函数。

一个describe里能够有多个it

异步测试

默认每一个测试用例最多执行2000毫秒,可使用-t参数指定超时时间

mocha -t 5000 test.js

it('should able to request https://api.github.com/', function (done) {
    // 使用 jQuery.ajax 请求
    $.ajax({
      url:"https://api.github.com/",
      success: function() {
        done()
      }
    })
  })
复制代码

异步测试时,须要在异步完成后加入done()告诉mocha测试结束。不然会一直等待,超时报错。

测试用例钩子

在describe中调用

  • before //在本区块的全部测试用例以前执行
  • after //在本区块的全部测试用例以后执行
  • beforeEach //在本区块的每一个测试用例以前执行
  • afterEach //在本区块的每一个测试用例以后执行

mocha还有其余不少功能,如

  • 监视指定测试用例,自动执行(mocha -w)

  • 用例管理(only,skip)

  • ...

只需前往Mocha查看文档

参考: 测试框架 Mocha 实例教程

测试执行过程管理工具

Karma

  1. 安装: npm install -g karma-cli
  2. 初始化: karma init

Which testing framework do you want to use? Press tab to list possible options. Enter to move to the next question.

mocha

Do you want to use Require.js? This will add Require.js plugin. Press tab to list possible options. Enter to move to the next question.

no

Do you want to capture a browser automatically? Press tab to list possible options. Enter empty string to move to the next question.

Chrome Firefox

What is the location of your source and test files? You can use glob patterns, eg. "js/*.js" or "test/**/*Spec.js". Press Enter to move to the next question.

node_modules/should/should.js test/**/*.js

Should any of the files included by the previous patterns be excluded? You can use glob patterns, eg. "**/*.swp". Press Enter to move to the next question.

Do you want Karma to watch all the files and run the tests on change? Press tab to list possible options.

yes

完成后会在项目根目录生成配置文件 karma.conf.js

  1. 启动测试: karma start

持续集成

持续集成指的是只要代码有变动,就自动运行构建和测试,反馈运行结果。

这里我采用了Travis CI

  1. 前往Travis CI绑定github帐号。
  2. 打开须要自动跑karma的项目。
  3. 项目根目录新建配置文件 .travis.yml

每次提交代码之后会根据配置文件的跑测试用例。不管是否跑通,都会有相应的报告。

参考:

持续集成服务 Travis CI 教程

使用Travis进行持续集成

相关文章
相关标签/搜索