sle*_*han 5 javascript unit-testing jasmine
我正在将大量的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();
};
Run Code Online (Sandbox Code Playgroud)
控制台输出示例:
Total: 67
Passed: 67
Failed: 0
Run Code Online (Sandbox Code Playgroud)
规范是Jasmine中的测试。在其中,您可以有类似于 其他测试框架中的断言的期望。因此,您看到报告的规格数量是每次调用的总数:it
it('passes some expectations', function () {
...
});
Run Code Online (Sandbox Code Playgroud)
通常,您会将几个类似单元的测试组合在一起it,这应该可以帮助您将功能组合在一起并提供有关应用程序开发方式的更连贯的视图。
现在,如果您坚持想了解规范中失败/成功的期望,您始终可以从报告者那里获取此信息。例如,如果您设置一个实例htmlReporter,您可以这样做:
htmlReporter.reportRunnerResults = function (runner) {
...
};
Run Code Online (Sandbox Code Playgroud)
在你的函数中,你可以检查各种各样的东西,这里有一些提示:
runner.specs()为您提供所有规格spec,results = spec.results()都会为您提供有关您的期望的信息。results.totalCount, results.failedCount,results.passedCount就是您正在寻找的;)| 归档时间: |
|
| 查看次数: |
2323 次 |
| 最近记录: |