【问题标题】:How to return Jasmine unit test results as a string?如何将 Jasmine 单元测试结果作为字符串返回?
【发布时间】:2012-11-13 18:14:31
【问题描述】:

我不确定 Reporters 是否缺少某些东西,但有没有一种简单的方法来执行我的单元测试并将结果作为字符串检索,而不是弄乱控制台或 DOM?

【问题讨论】:

    标签: javascript unit-testing jasmine


    【解决方案1】:

    为此,您必须实现自己的Reporter,它将记录结果并将其保存为文本格式。这是一个简短的示例,说明如何做到这一点:

    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";
    };
    

    之后,您可以使用TextReporter,而不是使用HtmlReporter

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

    如果您需要有关自定义报告器的更多信息,您只需要知道它们必须实现Reporter 接口。

    【讨论】:

    • 那么失败测试的预期值和当前值呢?
    猜你喜欢
    • 2013-12-29
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    相关资源
    最近更新 更多