简体   繁体   English

如何将Jasmine单元测试结果作为字符串返回?

[英]How to return Jasmine unit test results as a string?

我不确定Reporters是否遗漏了一些东西,但是有一种简单的方法可以执行我的单元测试并将结果检索为字符串而不是弄乱控制台或DOM吗?

To do this you have to implement your own Reporter that will log the result and keep it in a text format. 要做到这一点,你必须实现自己的Reporter ,它将记录结果并保持文本格式。 Here's a short example of how you can do it : 以下是如何做到这一点的简短示例:

function TextReporter() {
    this.textResult = "";
}

TextReporter.prototype = new jasmine.Reporter();

TextReporter.prototype.onRunnerFinished = function (callback) {
    this.callbackEnd = callback;
};

TextReporter.prototype.reportRunnerResults = function (runner) {        
    // When all the spec are finished //
    var result = runner.results();

    this.textResult += "Test results :: (" + result.passedCount + "/" + result.totalCount + ") :: " + (result.passed() ? "passed" : "failed");
    this.textResult += "\r\n";

    if (this.callbackEnd) {
        this.callbackEnd(this.textResult);
    }
};

TextReporter.prototype.reportSuiteResults = function (suite) {
    // When a group of spec has finished running //
    var result = suite.results();
    var description = suite.description;
}

TextReporter.prototype.reportSpecResults = function(spec) {
    // When a single spec has finished running //
    var result = spec.results();

    this.textResult += "Spec :: " + spec.description + " :: " + (result.passed() ? "passed" : "failed");
    this.textResult += "\r\n";
};

And after that instead of using the HtmlReporter , you can use your TextReporter . 而在这之后,而不是使用HtmlReporter ,您可以使用您TextReporter

var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;

var txtReporter = new TextReporter();
txtReporter.onRunnerFinished(function (text) {
    // Do something with text //
});

jasmineEnv.addReporter(txtReporter);

window.onload = function() {
    jasmineEnv.execute();
};

If you need more information about custom reporter, all you need to know is that they have to implement the Reporter interface. 如果您需要有关自定义报告者的更多信息,您需要知道的是他们必须实现Reporter界面。

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

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