【问题标题】:QUnit output: visual separation of modulesQUnit 输出:模块的视觉分离
【发布时间】:2009-10-11 12:52:54
【问题描述】:

我的测试可能如下所示:

module("some module");

test("test A", ...);
test("test B", ...);

module("other module");

test("test C", ...);
test("test D", ...);

QUnit 的输出将如下所示

1. test A (0, 0, 0)
2. test B (0, 0, 0)
3. test C (0, 0, 0)
4. test D (0, 0, 0)

是否可以让 QUnit 输出模块名称? 我很想拥有:

some module
1. test A (0, 0, 0)
2. test B (0, 0, 0)

other module
3. test C (0, 0, 0)
4. test D (0, 0, 0)

【问题讨论】:

    标签: javascript unit-testing qunit


    【解决方案1】:

    根据QUnit documentation,模块开始(和结束)有一个回调,此时您可以修改DOM。

    QUnit.moduleStart = function(name) {
      var tests = document.getElementById("qunit-tests");
    
      if ( tests ) {    
        var mod = document.createElement("h2");
        mod.innerHTML = name;
        tests.appendChild( mod );
      }
    };
    

    将东西放在列表中间有点不明智的 DOM 方式,但它似乎可以工作,至少在 FireFox 中是这样。

    【讨论】:

      【解决方案2】:

      Justin Love 的解决方案对我不起作用,但您可以在测试后插入以下 JQuery Javascript 以获得相同的预期结果:

      QUnit.done(function( details )
      {
          $("#qunit-tests > li ") //the test rows
          .each(function(index, Element){
              var moduleName = $(".module-name", this).text();
              if (moduleName !== $(".module-name", this.previousSibling).text())
              {
                  $(this).before( //prepend header to it
                      function(){
                          return "<h3 class='module-header'>" + moduleName + "</h3>"
                      ;})
              }
          });
      });
      

      【讨论】:

        猜你喜欢
        • 2021-06-27
        • 1970-01-01
        • 2021-07-01
        • 1970-01-01
        • 2011-03-24
        • 2015-01-23
        • 2016-12-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多