简体   繁体   English

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

[英]Display total number of tests/expectations run by Jasmine

I'm converting a large set of QUnit tests to Jasmine. 我正在将大量的QUnit测试转换为Jasmine。 In QUnit, I'm used to seeing the total number of tests across all test modules, displayed at the top. 在QUnit中,我习惯于看到所有测试模块的测试总数,显示在顶部。 Eg: 例如:

Tests completed in 157 milliseconds. 测试在157毫秒内完成。

528 tests of 528 passed, 0 failed. 528次测试528次通过,0次失败。

I consider the number of tests to be important information. 我认为测试的数量是重要的信息。 However, Jasmine's example test runners do not display the total number of tests. 但是,Jasmine的示例测试运行器不显示测试总数。 Rather, you get something like: 相反,你会得到类似的东西:

Passing 106 specs 通过106规格

Each of those specs may contain any number of individual tests. 这些规范中的每一个都可以包含任意数量的单独测试。 Is it possible to determine the total number of tests that have run, so that I can display it in my test runner? 是否可以确定已运行的测试总数,以便我可以在我的测试运行器中显示它? I've looked for information online and in the Jasmine docs but so far haven't been able to find anything. 我在网上和Jasmine文档中查找过信息,但到目前为止还没有找到任何信息。


Solution

Based on @ggozad's reply, I've come up with the following solution, which prints to the console. 根据@ ggozad的回复,我提出了以下解决方案,该解决方案将打印到控制台。 Suggestions for how to improve it or how to cleanly add the results to Jasmine's HTML output are welcome. 欢迎提出如何改进它或如何将结果干净地添加到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();
};

Example console output: 控制台输出示例:

Total: 67
Passed: 67
Failed: 0 

A spec is a test in Jasmine. 规范是Jasmine的测试 Inside it you can have expectations similar to assertions in other testing frameworks. 在其中,您可以获得类似于其他测试框架中的断言的 期望 So the number of specs you see reported are the total of each it calls: 因此,您看到的报告数量是it调用的总数:

it('passes some expectations', function () {
  ...
});

Typically you would group several unit-like tests together in an it , which should help you group functionality together and present a more coherent view of how your application is developing. 通常你需要将几个单元测试一样一起在it ,它会帮助你组功能组合在一起,并提出您的应用程序是如何制定更一致的看法。

Now, if you insist on wanting to know about the expectations that failed/succeeded in your spec, you can always get this information from your reporter. 现在,如果您坚持想要了解您的规范中失败/成功的期望,您始终可以从您的记者那里获取此信息。 For instance if you setup up an instance of an htmlReporter , you could do: 例如,如果您设置了htmlReporter的实例,则可以执行以下操作:

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

Inside your function you can check all sorts of things, here are some hints: 在你的功能里面你可以查看各种各样的东西,这里有一些提示:

  • runner.specs() gives you all the specs runner.specs()为您提供所有规格
  • for each of those say spec , results = spec.results() would give you info about your expectations. 对于每个说specresults = spec.results()会给你关于你的期望的信息。
  • results.totalCount , results.failedCount , results.passedCount is what you are seeking ;) results.totalCountresults.failedCountresults.passedCount就是你要找的;)

Very useful information. 非常有用的信息。 You might want to append it to the page rather than writing it to the console. 您可能希望将其附加到页面而不是将其写入控制台。 You can replace your console script with... 您可以用...替换您的控制台脚本

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM