Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 显示Jasmine运行的测试/期望总数_Javascript_Unit Testing_Jasmine - Fatal编程技术网

Javascript 显示Jasmine运行的测试/期望总数

Javascript 显示Jasmine运行的测试/期望总数,javascript,unit-testing,jasmine,Javascript,Unit Testing,Jasmine,我正在将大量的QUnit测试转换为Jasmine。在QUnit中,我习惯于看到顶部显示的所有测试模块的测试总数。例如: 测试在157毫秒内完成 528项测试中有528项通过,0项失败 我认为测试的数量是重要的信息。但是,Jasmine的示例测试运行程序不显示测试的总数。相反,你会得到如下结果: 通过106个规格 每个规范可能包含任意数量的单独试验。是否可以确定已运行的测试总数,以便在测试运行程序中显示它?我在网上和Jasmine文档中查找信息,但到目前为止还没有找到任何信息 解决方案 根据@g

我正在将大量的QUnit测试转换为Jasmine。在QUnit中,我习惯于看到顶部显示的所有测试模块的测试总数。例如:

测试在157毫秒内完成

528项测试中有528项通过,0项失败

我认为测试的数量是重要的信息。但是,Jasmine的示例测试运行程序不显示测试的总数。相反,你会得到如下结果:

通过106个规格

每个规范可能包含任意数量的单独试验。是否可以确定已运行的测试总数,以便在测试运行程序中显示它?我在网上和Jasmine文档中查找信息,但到目前为止还没有找到任何信息


解决方案 根据@ggozad的回复,我提出了以下解决方案,并将其打印到控制台。对于如何改进它或如何将结果干净地添加到Jasmine的HTML输出中,欢迎提出建议

var jasmineEnv = jasmine.getEnv();
var htmlReporter = new jasmine.HtmlReporter();
var reportRunnerResults = htmlReporter.reportRunnerResults;

htmlReporter.reportRunnerResults = function(runner) {
    reportRunnerResults(runner);

    var specs = runner.specs();
    var specResults;
    var assertionCount = {total: 0, passed: 0, failed: 0};

    for (var i = 0; i < specs.length; ++i) {
        if (this.specFilter(specs[i])) {
            specResults = specs[i].results();
            assertionCount.total += specResults.totalCount;
            assertionCount.passed += specResults.passedCount;
            assertionCount.failed += specResults.failedCount;
        }
    }

    if (console && console.log) {
        console.log('Total: ' + assertionCount.total);
        console.log('Passed: ' + assertionCount.passed);
        console.log('Failed: ' + assertionCount.failed);
    }
};

jasmineEnv.addReporter(htmlReporter);
jasmineEnv.specFilter = function(spec) {
    return htmlReporter.specFilter(spec);
};

window.onload = function() {
    jasmineEnv.execute();
};
规范是对茉莉花的测试。在它里面,您可以有类似于其他测试框架中的断言的期望。因此,您看到报告的规格数是每个
it
调用的总数:

it('passes some expectations', function () {
  ...
});
通常,您会在一个
it
中将几个类似单元的测试组合在一起,这将有助于您将功能组合在一起,并对应用程序的开发方式提供一个更加一致的视图

现在,如果你坚持想知道在你的规范中失败/成功的期望值,你总是可以从你的记者那里得到这些信息。例如,如果您设置了一个
htmlReporter
的实例,您可以执行以下操作:

htmlReporter.reportRunnerResults = function (runner) {
...
};
在您的函数中,您可以检查各种情况,以下是一些提示:

  • runner.specs()
    提供所有规格
  • 对于每个人来说,
    spec
    results=spec.results()
  • results.totalCount
    results.failedCount
    results.passedCount
    是您要查找的;)
    非常有用的信息。您可能希望将其附加到页面,而不是将其写入控制台。您可以将控制台脚本替换为

    var d1 = document.createElement("span");
    $(d1).css('font-size', '10pt');
    $(d1).html(' (Total Expectations: ' + assertionCount.total);
    $(d1).append(', Total Expectations Passed: ' + assertionCount.passed);
    $(d1).append(', Total Expectations Failed: ' + assertionCount.failed);
    $(d1).append(')');
    $(".passingAlert").append(d1);
    $(".resultsMenu").append(d1);
    

    我想我已经很清楚了,我是在试图确定已经运行的预期数量(通过
    expect
    ),而不是已经运行的规范数量(
    it
    )。@slevithan好了,就这样;)感谢您在编辑中添加的详细信息!我已经编辑了我的原始问题,根据您的答案添加了我的解决方案。我是Jasmine的新手,欢迎提出任何改进建议。:)
    var d1 = document.createElement("span");
    $(d1).css('font-size', '10pt');
    $(d1).html(' (Total Expectations: ' + assertionCount.total);
    $(d1).append(', Total Expectations Passed: ' + assertionCount.passed);
    $(d1).append(', Total Expectations Failed: ' + assertionCount.failed);
    $(d1).append(')');
    $(".passingAlert").append(d1);
    $(".resultsMenu").append(d1);