【问题标题】:mocha: How to reduce printed time when using setTimeoutmocha:使用 setTimeout 时如何减少打印时间
【发布时间】:2016-10-17 08:20:39
【问题描述】:

我使用mocha --slow 0 ./test/test.js 运行 mocha 以显示完成呼叫所需的时间。我正在编写一个库,它与另一个服务和响应时间很重要。但是,我的库中的一些调用会导致另一个服务执行缓慢的操作,例如 startstopremove 更多时间来完成然后存在于两个之间连续测试。结果是显示 remove 测试的打印时间比调用完成所需的实际时间长。

describe(`stopContainer ${test_name} t:0`, () => {
  it(`should stop the container named ${test_name}`, () => {
    return engine.stopContainer(test_name, {t:0}).should.be.fulfilled
  })
})

// this shows to be much longer than the actual call takes
describe(`removeContainer ${test_name} v:1 `, () => {
  it(`should remove the container named ${test_name} & volumes`, function(done) {
    // needs to wait a second because of engine latency
    this.timeout(5000)
    setTimeout(() => {
      engine.removeContainer(test_name, {v:1}).should.be.fulfilled.and.notify(done);
    }, 1000)
  })
})

打印以下内容

  stopContainer dap_test_container t:0
    ✓ should stop the container named dap_test_container (285ms)
  removeContainer dap_test_container v:1
    ✓ should remove the container named dap_test_container & volumes (1405ms)

显然最后一次测试比报告的时间少了 1000 毫秒。但是我必须在整个地方进行数百次测试,因此报告的值变得更加没有意义,因为我无法跟踪哪些被延迟,哪些没有。

注意我并不是想以此作为我的代码分析方法,这只是为了让我的测试结果更有意义。

我想减少打印时间,mocha 有没有办法手动减少打印时间?或者 mocha 是否为此提供了更好的结构?

【问题讨论】:

  • 它显示了很长时间,因为它需要很长时间-集成测试就是这样。如果您不想为集成测试计时,请不要编写一个-不要在您实际测试的内容之外做任何事情。
  • 同意 - 为什么不使用例如Sinon.js 将外部调用排除在外吗?测试外部调用通常是没有意义的,因为您实际上只是在测试它们“在测试时”是否工作,而不能保证它们会在一分钟后工作。相反,您可以使用存根来模拟它们的预期行为......
  • 我不同意——你们都在自己承担很多。这些测试不是生产系统的一部分或任何类似的东西。这只是为了让我可以做我的日常工作。此外,它还与 docker-engine 远程 api 接口,该 api 今天有 1800 个未解决的问题,并非所有问题都是错误,但我肯定会在“外部服务”中发现错误并积极贡献这些错误报告以改进它。还有一点,建议这些 cmets 完全还原了这个问题。
  • @MatUtter :无意冒犯。但我确实认为在观察关注点分离原则时,可以获得最高质量 - 在测试时也是如此。因此,当测试外部服务有意义时,我会亲自为此制作一套测试服,否则在测试我自己的代码时将其剔除。
  • 你仍然没有抓住重点。讨论 TDD 和回归测试的原则与问题无关。如果我想问这个,我会的。我不是。我正在尝试使用其他服务 API。关于 mocha 有一个非常明确的问题,这两个回答都忽略了。

标签: javascript node.js asynchronous mocha.js


【解决方案1】:

很简单,延迟在before或者after

我做了一个全局调用等待

global.wait = wait = function(ms) {
  return new Promise(resolve => {
    setTimeout(resolve, ms)
  })
}

并像这样在测试中使用它

describe(`removeContainer ${test_name} v:1 `, () => {

  before('wait for latency', () => wait(1000))

  it(`should remove the container named ${test_name} & volumes`, () => {
    return engine.removeContainer(test_name, {v:1}).should.be.fulfilled;
  })
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    • 2010-10-23
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    相关资源
    最近更新 更多