【问题标题】:Run mocha tests individually单独运行 mocha 测试
【发布时间】:2012-04-06 12:30:18
【问题描述】:

我的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

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


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

谢谢。

【问题讨论】:

  • 希望我能正确理解您的问题,但您不能为此使用闭包吗?

标签: node.js mocha.js


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    如果您正在测试的代码的行为因某些全局状态而异,那么您必须在测试中对其进行控制。你可以编写一些“beforeEach”函数来设置你需要的任何全局状态。

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

    【讨论】:

      【解决方案3】:

      全局状态通常会使测试变得困难。 googletechtalks 的 Clean Code Talks 值得任何有兴趣编写可测试软件的人观看

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-27
        • 2018-12-02
        • 2020-08-26
        • 2015-12-27
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        相关资源
        最近更新 更多