单独运行摩卡testing
我的test/
项目充满了摩卡testing:
test/ ├── a.coffee └── b.coffee
说,一个a.coffee
是
console.log 'Executing A test!' global.foo = 'FOO'
和b.coffee
是
console.log 'Executing B test!' console.log 'Expecting foo not defined:', assert.equal(foo, undefined)
在执行摩卡时:
$ mocha --compilers coffee:coffee-script test/* Executing A test! Executing B test! Expecting foo not defined: false
它看起来像testing共享相同的全球对象(我想避免)…
有没有办法单独执行每个testing?
谢谢。
摩卡的作者在这里回答了这个问题: https : //github.com/visionmedia/mocha/issues/365#issuecomment-4997480
如果您正在testing的代码在某些全局状态下performance有所不同,那么您必须将其作为testing的一部分进行控制。 你可以编写一些“beforeEach”函数来设置你需要的全局状态。
您可能还想考虑重新考虑代码,以使其不依赖于全局状态。 如果你可以传递参数或其他东西(显式或隐式),那么你可以testing更多的信心。
全球状况总体上使testing变得困难。 任何有兴趣编写可testing软件的人都应该关注googletechtalks的Clean Code Talks
- unit testing
- 全局状态和单身
- 不要找东西