Display total Jasmine tests / expectations

I am converting a large set of QUnit tests to Jasmine. In QUnit, I'm used to seeing the total number of tests in all test modules displayed on top. For instance:.

Tests completed in 157 milliseconds.

528 out of 528 tests passed, 0 failed.

I believe the number of tests is important information. However, sample Jasmine test runners do not show the total number of tests. Rather, you will get something like:

Transfer 106 specifications

Each of these specifications may contain any number of individual tests. Is it possible to determine the total number of running tests so that I can display it in my test runner? I searched on the Internet and in Jasmine docs, but still could not find anything.


Decision

Based on @ggozad's answer, I came up with the following solution, which prints to the console. Suggestions on how to improve it or how to cleanly add results to the Jasmine HTML output are welcome.

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 
+4
source share
2 answers

The specification is a test in Jasmine. Inside it, you can expect expectations similar to statements in other testing structures. So the number of specs displayed is the total number of calls to it :

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

Typically, you combine several tests, like a module, into it , which should help you combine the functionality together and provide a more consistent view of how your application is developed.

Now, if you insist on wanting to know about the expectations that failed / succeeded in your specification, you can always get this information from your reporter. For example, if you configured an instance of htmlReporter , you can do:

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

Inside your function you can check all kinds of things, here are some tips:

  • runner.specs() gives you all the specifications
  • for each of those who say spec , results = spec.results() will provide you with information about your expectations.
  • results.totalCount , results.failedCount , results.passedCount is what you are looking for;)
+3
source

Very helpful information. You might want to add it to the page rather than writing it to the console. You can replace the 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); 
0
source

All Articles