mocha

Mocha是一个拥有丰富功能的javascript测试框架,可以用于nodejs和浏览器。支持同步/异步测试用例,有多种报告形式。

Installation

npm install -g mocha

Assertions

  • should.js BDD style shown throughout these docs.
  • chai expect() assert() and should style assertions
  • expect.js expect() style assertions
  • better-assert c-style self.documenting assert()

我一般使用nodejs自带的assert模块和should.js

Synchronous code

1
2
3
4
5
describe 'Array',->
describe '#indexOf()',->
it 'should return -1 when the value is not present.',->
[1,2,3].indexOf(5).should.equal -1
[1,2,3].indexOf(0).should.equal -1

Asynchronous code

1
2
3
4
5
6
7
describe 'User',->
describe '#save()',->
it 'should save without error',(done)->
user = new User 'Luna'
user.save (err)->
assert.ifError err
done()

可以看到同步/异步的区别只是增加一个done函数作为参数。异步过程结束后调用done()即可。

done()函数也可以接受一个error作为参数。

hooks

具体用法看名字就知道了。

  • before()
  • after()
  • beforeEach()
  • afterEach()

All ‘hooks’ 同样可以接受done参数用于同步/异步过程。

甚至可以不使用done回调,而是直接返回一个promise对象。例如:

1
2
3
it 'test',->
db.clear().then ->
return db.save [tobi,loki,jane]

不过一般还是习惯加上done参数,整个流程看起来更清晰。

也可以什么都不做,只加入一段说明或者等待以后补充处理过程。

1
2
3
describe 'Array',->
descibe '#indexOf',->
it 'should return -1 when the value is not present.'

Exclusive tests

通过使用only()只执行指定的测试场景。

1
2
3
describe 'Array',->
describe.only '#indexOf()',->
# do something
1
2
3
4
5
describe 'Array',->
describe '#indexOf()',->
it.only 'should return -1 unless present.',->

it 'should return the index when present.',->

Inclusive tests

类似only(),可以用skip()跳过某些测试。

1
2
3
4
5
describe 'Array',->
describe.skip '#indexOf',->
# do something
describe '#ooo',->
it.skip 'shoud...',->

基本用法和命令参数

mocha [debug] [options] [files]

直接执行mocha命令,默认会查找./test/*.js,so,尽量把所有测试用例放到test目录。

因为现在基本只用coffee编码,为了偷懒少打几个字,习惯将常用的mocha命令加入到grunt tasks中,安装grunt-shell模块,再添加一个简单的task即可,这里就不提了。

mocha init <path>

指定一个目录,初始化一个用于浏览器的测试目录。执行后会在该目录生成index.html,mocha.js,mocha.css和一个空白的test.js,可以直接在test.js在添加测试用例。

命令行

  • -h,—help
  • -V,—version
  • -A,—async-only 强制为异步模式,即所有测试必须包含一个done()回调。
  • -c,—colors
  • -C,—no-colors
  • -G,—growl
  • -O,—reporter-options \
  • -R,—reporter \
  • -S,—sort
  • -b,—bail
  • -d,—debug
  • -g,—grep \
  • -f,—fgrep \
  • -gc,—expose-gc
  • -i,—invert
  • -r,—require \
  • -s,—slow \
  • -t,—timeout \ 设置超时,默认为2000ms,如果是长时间运算需要设置。
  • -u,—ui \
  • -w,—watch 监视文件变化
  • —check-leaks
  • —compilers \:\,… 使用指定模块编译文件,经常会用到编译coffee
  • —debug-brak
  • —globals \
  • —inline-diffs
  • —interfaces
  • —no-deprecation
  • —no-exit
  • —no-timeouts
  • —opts \
  • —prof
  • —recursive
  • —reporters
  • —throw-deprecation
  • —trace
  • —trace-deprecation
  • —watch-extensions \,…
  • —delay

常用

-w 监视文件变化

—compilers

for coffeescript 1.6

mocha --compilers coffee:coffee-script test/test.coffee

for coffeescript 1.7+

mocha --compilers coffee:coffee-script/register test/test.coffee

-b

只显示第一个异常,一般测试用例非常多的时比较有用,不然某个接口变化后全屏报错就会悲剧。

-t

指定超时时间,命令行中添加为全局的设置,也可以在测试用例内部调用timeout()方法单独设置。

1
2
3
4
describe 'a suite of tests.',->
this.timeout 500
it 'should take less than 500ms',(done)->
setTimeout done,300

Interfaces

BDD

我一般只用到BDD
BDD interface 提供了 describe(),context(),it(),before(),after(),beforeEach(),afterEach()
context()describe()没区别。

TDD

TDD interface 提供了suite(),test(),suiteSetup(),suiteTeardown(),setup(),teardown()

Exports

这个没什么好讲的。

1
2
3
4
5
6
module.exports =
before:->
'Array':
'#indexOf()':->
'should return -1 when not present':->
[1,2,3].indexOf(4).should.equal -1

QUnit

用到的第一个测试框架就是QUnit,用法非常简单,但是现在基本不用了,没有层次会比较乱。