【问题标题】:How to get information of Mocha #<Test> type object如何获取 Mocha #<Test> 类型对象的信息
【发布时间】:2013-12-02 12:18:35
【问题描述】:

我在浏览器上使用 mocha。

在函数内部我有一个名为 test 的对象

it('', function(done){
                console.log(this.test);
                  done(error);
              });

如果我们检查日志,我们会得到 this.test 一个“测试”类型的对象。

Test {title: "", fn: function, async: 1, sync: false, _timeout: 50000…}

如果我控制台 this.test.duration 或我发现未定义的任何变量。

我想提取这个“测试”类型对象的持续时间信息。

如何做到这一点..??

【问题讨论】:

    标签: javascript mocha.js testcase


    【解决方案1】:

    只有在测试真正结束并完成后,您才能访问this.test 的属性。 因此,为了获取这些数据,您需要在登录控制台之前调用 done();。

    it("is a test", function(done) {
        done();
        console.log(this.test.title);
        console.log(this.test.state);
    });
    >> "is a test"
    >> "passed"
    

    【讨论】:

      【解决方案2】:

      我已经阅读了 mocha 的源代码,但我没有看到从测试用例中提取您想要的信息的方法测试正在运行。 duration 字段在测试结束时计算一次。

      您仍然可以计算出您的测试已经运行了多长时间:

      it('', function(done){
          var started_at = new Date;
          [... do stuff ...]
          console.log("duration so far:" , (new Date - started_at) + "ms");
          [... do more stuff ...]
          done(error);
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-26
        • 2014-01-07
        • 2018-01-19
        • 1970-01-01
        • 2015-06-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多