单独运行mocha测试

时间:2012-04-06 12:30:19

标签: node.js mocha

我的test/项目充满了mocha次测试:

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时:

$ mocha --compilers coffee:coffee-script test/*
Executing A test!
Executing B test!
Expecting foo not defined: false

看起来测试共享同一个全局对象(我想避免)...


有没有办法单独执行每个测试?

谢谢。

3 个答案:

答案 0 :(得分:1)

Mocha的作者在这里回答了这个问题:https://github.com/visionmedia/mocha/issues/365#issuecomment-4997480

答案 1 :(得分:0)

如果您测试的代码根据某些全局状态而表现不同,那么您必须将其作为测试的一部分来控制。您可以编写一些“beforeEach”函数来设置您需要的全局状态。

您可能还想考虑重新分解代码,使其不依赖于全局状态。如果您可以传递参数或其他内容(显式与隐式),那么您可以更自信地进行测试。

答案 2 :(得分:0)

全球一般情况下,测试很困难。任何有兴趣编写可测试软件的人都值得关注googletechtalks的清洁代码会谈

  1. Unit Tests
  2. Global State and Singletons
  3. Don't Look For Things
相关问题