单元测试学习总结

0次阅读

共计 3695 个字符,预计需要花费 10 分钟才能阅读完成。

Mocha 的安装和使用
1. 安装
使用 npm 全局安装:
npm install -g mocha
安装 Mocha >= v3.0.0,npm 的版本应该 >=v1.4.0。除此,确保使用 Node.js 的版本 >=v0.10 来运行 Mocha
2. 简单的例子
https://github.com/xzq8/exerc…
先 fork 代码到自己仓库,然后 clone 下来。安装依赖:
git clone url
npm i
npm test(package.json 中的 script) // 执行 mocha
使用 nodejs 的断言 assert, 下面是一个最基本的例子:···
var assert = require(‘assert’)

describe(‘Array’, function() {
describe(‘#indexOf()’, function() {
it(‘should return -1 when the value is not present’, function() {
assert.equal(-1, [1, 2, 3].indexOf(5)/* 填空题 */)
})
})
})
···
- 了解 Node assert 的用法 文档 http://nodejs.cn/api/assert.html
assert.deepStrictEqual(actual, expected[, message]) // 判断深度 {a:{a:1}} == {b:{c:1}}}
assert.strictEqual(actual, expected[, message]) // 判断相等 ===
assert.notStrictEqual(actual, expected[, message]) // 判断不等 ===
assert.ok(value[, message]) // 是否为真
assert.throws(()=>{
throw new Error(error)
},Error,’ 不符合预期的错误 ’) // 抛出错误
以上是几个常用的 assert 断言。
异步的代码如何使用断言呢?
describe(‘jquery’, function() {
describe(‘ 异步断言测试 ’, function() {
it(‘ 异步需要执行回调 done()’, function() {
$.ajax({
url:”a.html”,
success:function(){
done();
}
})
})
})
})
使用 mocha 测试异步代码是再简单不过了。只需要在测试完成的时候调用一下回调函数即可。通过添加一个回调函数 (通常命名为 done) 给 it()方法,Mocha 就会知道,它应该等这个函数被调用的时候才能完成测试。
此外 Mocha 提供了一些钩子函数:before(),after(),beforeEach()和 afterEach()。这些钩子函数可以用于设置测试的先决条件或者对测试进行清理。
describe(‘hooks’, function() {
before(function() {
// 在这个区块内的所有测试之前运行
})
after(function () {
// 在这个区块内的所有测试之后运行
})
beforeEach(function () {
// 在这个区块内的每个测试运行之前运行
})
afterEach(function () {
// 在这个区块内的每个测试之后运行
})
})
测试可以出现在 before,after 或者和你的钩子函数交替出现。钩子函数会按照它们被定义的顺序运行。一般就是,before()(只运行一次)->beforeEach()->afterEach()->after()(只运行一次)。
mocha 的命令的基本选项:
Options:

-h, –help 输出帮助信息
-V, –version 输出 mocha 的版本号
-A, –async-only 强制所有的测试用例必须使用 callback 或者返回一个 promise 的格式来确定异步的正确性
-c, –colors 在报告中显示颜色
-C, –no-colors 在报告中禁止显示颜色
-g, –growl 在桌面上显示测试报告的结果
-O, –reporter-options <k=v,k2=v2,…> 设置报告的基本选项
-R, –reporter <name> 指定测试报告的格式
-S, –sort 对测试文件进行排序
-b, –bail 在第一个测试没有通过的时候就停止执行后面所有的测试
-d, –debug 启用 node 的 debugger 功能
-g, –grep <pattern> 用于搜索测试用例的名称,然后只执行匹配的测试用例
-f, –fgrep <string> 只执行测试用例的名称中含有 string 的测试用例
-gc, –expose-gc 展示垃圾回收的 log 内容
-i, –invert 只运行不符合条件的测试用例,必须和 –grep 或 –fgrep 之一同时运行
-r, –require <name> require 指定模块
-s, –slow <ms> 指定 slow 的时间,单位是 ms,默认是 75ms
-t, –timeout <ms> 指定超时时间,单位是 ms,默认是 200ms
-u, –ui <name> 指定 user-interface (bdd|tdd|exports)中的一种
-w, –watch 用来监视指定的测试脚本。只要测试脚本有变化,就会自动运行 Mocha
–check-leaks 检测全局变量造成的内存泄漏问题
–full-trace 展示完整的错误栈信息
–compilers <ext>:<module>,… 使用给定的模块来编译文件
–debug-brk 启用 nodejs 的 debug 模式
–es_staging 启用全部 staged 特性
–harmony<_classes,_generators,…> all node –harmony* flags are available
–preserve-symlinks 告知模块加载器在解析和缓存模块的时候,保留模块本身的软链接信息
–icu-data-dir include ICU data
–inline-diffs 用内联的方式展示 actual/expected 之间的不同
–inspect 激活 chrome 浏览器的控制台
–interfaces 展示所有可用的接口
–no-deprecation 不展示 warning 信息
–no-exit require a clean shutdown of the event loop: mocha will not call process.exit
–no-timeouts 禁用超时功能
–opts <path> 定义 option 文件路径
–perf-basic-prof 启用 linux 的分析功能
–prof 打印出统计分析信息
–recursive 包含子目录中的测试用例
–reporters 展示所有可以使用的测试报告的名称
–retries <times> 设置对于失败的测试用例的尝试的次数
–throw-deprecation 无论任何时候使用过时的函数都抛出一个异常
–trace 追踪函数的调用过程
–trace-deprecation 展示追踪错误栈
–use_strict 强制使用严格模式
–watch-extensions <ext>,… –watch 监控的扩展
–delay 异步测试用例的延迟时间
更多 mocha 相关参考《https://segmentfault.com/a/11…》里面介绍的比较详细。
should.js 的使用
在 test 目录添加 mocha.opts 文件:
–require should // mocha 在执行的时候回引入 shouldjs
sholdjs 的常用的几个:
a.should.equal(b) // 判断相等
(5).should.be.exactly(5).and.be.a.Number() //
should.throws(block, [error], [message]) // 捕获错误
具体更多断言参考 http://shouldjs.github.io/
接入 travis Ci
打开:<https://www.travis-ci.org/>
登录 GitHub 账号 授权

在项目根目录添加.travis.yml 文件
language: node_js // 执行的语言
node_js:
– “8.9.4” // 版本
before_script: // 钩子 有 before 肯定就有 after
– npm install -g mocha
这样当你提交代码之后 travis 就会自动运行测试 发送报告到你的邮箱
karma 的基本使用
参考《https://www.jianshu.com/p/66d…》
个人理解就是把我们测试过程中编写的测试用例,通过它调用浏览器来运行这些测试用例,然后再汇集测试结果,生成测试报告!
需要注意的就是 travis 的虚拟机没有 chrome,所以需要进行配置
language: node_js
node_js:
– “8.9.4”
before_script:
– npm install -g karma-cli
– “export DISPLAY=:99.0”
– “sh -e /etc/init.d/xvfb start”

正文完
 0