首页 文章

在Jasmine测试中将对console.log()的调用重定向到标准输出

提问于
浏览
59

我通过jasmine-maven-plugin使用Jasmine,我想在Maven构建输出中看到console.log()消息 . 有没有办法实现这个目标?

如果无法重定向console.log(),是否还有其他方法可以从我的测试中进行记录,以便它们显示在Maven构建输出上?

我正在以无头的方式在Jenkins上运行这些测试,并希望能够从测试中获得一些调试输出 .

8 回答

  • 11

    我认为 is not possible .

    我不得不在规范加载器中覆盖console.log实现 . 即(使用jQuery):

    var console = {
        panel: $('body').append('<div>').css({position:'fixed', top:0, right:0,background:'transparent'}),
        log: function(m){
            this.panel.prepend('<div>'+m+'</div>');
        }       
    
    };
            console.log('message 1');
            console.log('message 2');
    

    here你有一个功能性的例子

  • 1

    如果您迫切希望终端窗口中的任何输出,以便在测试完成并且浏览器关闭后可以查看数据, console.error() 似乎可以解决问题 .

  • 2

    尝试

    console.info('foo')
    

    来自测试javascripts .

  • 53

    您可以使用:

    jasmine.log("I've got a big log.");
    

    NB: See douglas-treadwell's comment below.

    这是指Jasmine 1.x.使用Jasmine 2.0,只需直接使用console.log .

  • 3

    如果您在节点环境中运行 . 你可以用

    process.stdout.write(“这将发送到控制台”);

  • 37

    我正在使用 jasmine 2 来自 guardphantom js 并且发现测试中的标准 console.log 消息输出到jasmine spec runner的控制台就好了 .

    我还发现,我正在测试的javascript代码元素中的 console.log 消息会在测试本身内写入 stdout 但不会 console.log 消息 .

  • 3

    使用grunt / karma / jasmine(karma-jasmine 0.2.2)进入相同的问题 -

    为了配合Dave Sag说的话,我发现我测试的代码中的所有 console.log 消息都运行正常,但是我的 describe() {}it() {} 块没有记录任何内容 .

    我确实发现你可以从 beforeEach() {} 块登录 . 至少它对我有用:

    beforeEach(function() {
      this.model = new blahModel();
      console.log('this.model = ', this.model);
    });
    

    请注意,这仅在浏览器控制台中登录,并且出于某种原因赢得't log in command line. Somewhat strange when the console.log statements from the tested code block do log in the command line. I' ve也发现了一种更好的方法来进行一致的日志记录here .

    更新:我实际上看到日志工作也阻塞它,我相信我有其他错误阻止了这一点 .

  • 3

    1)转到您拥有pom.xml的项目目录 . 在cmd中运行以下命令 . mvn jasmine:bdd

    2)您将获得localhost URL:localhost:8234(仅作为示例) .

    3)在浏览器中运行此URL . 现在所有的测试用例都被执行了 .

    4)执行此页面的Inspect元素 . 在浏览器控制台中,您将能够看到所有console.log()或console.error()跟踪 .

相关问题