分离unit testing和集成testing

我有兴趣创build完整的模拟unit testing,以及集成testing,检查是否一些asynchronous操作已正确返回。 我想要一个unittesting的命令,一个用于integrationtesting的命令,这样我可以在我的CI工具中单独运行它们。 什么是最好的方法来做到这一点? 像摩卡这样的工具和玩笑似乎只关注一种做事方式。

我看到的唯一select是使用摩卡,并在目录中有两个文件夹。

就像是:

  • __unit__
  • __integration__

然后,我需要一些方法告诉mocha运行src目录中的所有__unit__testing,另一个告诉它运行所有的__integration__testing。

思考?

摩卡支持文件通配符和testing名称grepping ,它可以用来创buildtesting的“组”。

文件前缀

 test/unit_whatever_spec.js test/int_whatever_spec.js 

然后用文件运行

 mocha test/unit_*_spec.js mocha test/int_*_spec.js mocha 

目录

 test/unit/whatever_spec.js test/int/whatever_spec.js 

然后运行目录

 mocha test/unit mocha test/int mocha test/**/*_spec.js 

testing名称

 describe 'Unit::Whatever', -> describe 'Integration::Whatever', -> 

然后运行命名块

 mocha -g ^Unit:: mocha -g ^Integration:: mocha 

在使用testing名称时仍然保留文件前缀分隔是有用的,所以你可以很容易地跟踪testing的来源。

NPM

将每个testing命令存储在package.json scripts部分,这样就可以很容易地运行npm run test:int

 { scripts: { "test": "mocha", "test:unit": "mocha -g ^Unit::", "test:int": "mocha -g ^Integration::" } } 

摩卡不支持标签或类别。 你理解正确。 你必须创build两个文件夹,单元和集成,并像这样调用摩卡

摩卡单位摩卡一体化