javascript - QUnit Javascript 单元测试显示断言号而不是测试号

标签 javascript unit-testing qunit

希望这是我错过的一个配置选项,但我已经检查了文档,但找不到关于它的任何信息...

我希望 QUnit 显示通过/失败的测试的数量,而不是通过/失败的断言的数量。

目前显示:

13 assertions of 38 passed, 25 failed.

如图所示: Qunit

这很烦人,因为我想跟踪我编写了多少测试,而在 QUnit 网站上它实际上显示了我想要的: enter image description here

从更改日志来看,这似乎是在 1.11.0 中更改的。有没有人对如何在不破解源代码(或破解源代码 - 尽管这可能会通过 github 提出和添加...)的情况下将其改回有任何建议?

最佳答案

简短的回答:这是不可能的。事实上,它似乎从来都不可能。

来自变更日志:

Rename tests to assertions in summary. Fixes #336 - Summary counts assertions but mentions 'tests'.

QUnit 一直在计算并输出通过/失败的断言数量。但是,在传递中,它在应该说“断言”的地方说了“测试”。如果您重新阅读变更日志,它会说它已从“测试”更改为“断言”作为修复,因为这是它始终应该说的。

关于您的后续问题,是否可以破解源代码?

查看代码,第 1269 到 1279 行似乎是生成输出的地方:

html = [
    "Tests completed in ",
    runtime,
    " milliseconds.<br/>",
    "<span class='passed'>",
    passed,
    "</span> assertions of <span class='total'>",
    config.stats.all,
    "</span> passed, <span class='failed'>",
    config.stats.bad,
    "</span> failed."

所以它将通过/失败的次数存储在 config.stats 对象中。不幸的是,从我在源代码中读到的内容来看,它只存储与断言相关的统计信息,而不是测试。大概可以添加某种黑客代码来存储失败的测试,但是这需要对现有源代码有很好的了解,所以我不建议这样做。

正如您所说,您最好的选择是在 GitHub 上提出建议,但我不知道您在不久的将来(如果全部)添加此功能的机会有多大,因为断言的数量表明失败(通常)比测试次数更有值(value)。

更新:我对源代码进行了更多的搜索,因为我很好奇它有多难。尽管我无法想出任何简单的方法来将此功能添加到代码中,但我确实想到了解决您问题的替代方案。

QUnit 有一个名为 QUnit.testDone() 的方法如果您实现它,它将在每次测试完成后自动调用。您可以使用它在每次测试后手动增加测试计数器,并输出到 Chrome 的开发者控制台。以下是如何执行此操作的示例:

<!DOCTYPE html>
<html>
<head>
    <title>Track Number of Tests Manually</title>
    <link type="text/css" rel="stylesheet" href="qunit-1.12.0.css" />
    <script type="text/javascript" src="qunit-1.12.0.js"></script>
    <script type="text/javascript" src="jquery-1.10.2.min.js"></script>
</head>
<body>
    <div id="qunit"></div>
    <div id="qunit-fixture">
        <input type="text" name="aninput" id="aninput" />
    </div>
    <script>
        // Track number of tests run and failed.
        var numTests = 0;
        var failedTests = 0;

        // Outputs the number of failed tests so far. If the console is not cleared each time, then the last message will be the final count.
        QUnit.testDone(function( details ) {
            // If any previous messages in console, clear them
            console.clear();
            // Increment number of tests run
            numTests++;
            // If any assertions have failed, increment number of failed tests
            if (details.failed > 0)
                failedTests++;
            var passed = numTests - failedTests;
            // Output to console.
            console.log( 'Passed Tests: '+passed+'\nFailed Tests: '+failedTests+'\nTotal Tests: '+numTests);
        });

        test( "a test", function() {
            equal( $('#aninput').val(), '', 'Initially empty');
            $('#aninput').val('sometext');
            equal( $('#aninput').val(), 'sometext', 'Input text now "sometext"');
        });

        test( "another test", function() {
            equal( $('#aninput').val(), '', 'Initially empty');
            $('#aninput').val('sometext');
            equal( $('#aninput').val(), 'some text', 'Input text now "some text"'); // Expect to fail, as there is no space in the input
            $('#aninput').val(' ');
            equal( $('#aninput').val(), '', 'Input should now be empty'); // Expect to fail as was actually set to whitespace, not empty
        });
    </script>

</body>
</html>

我已经测试了这段代码,它似乎可以解决问题。我知道这不是您想要的解决方案,但它确实为您提供了所需的信息,而无需尝试破解 QUnit 源代码! :)

关于javascript - QUnit Javascript 单元测试显示断言号而不是测试号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20240034/

相关文章:

javascript - 隐藏 DIV 并在提交时显示图像

javascript - react 表过滤和响应

javascript - angularjs [ng :areq] Argument 'fn' is not a function, 在控制台错误中得到字符串

Angular 项目 : Chrome debugger not working correctly for unit tests

javascript - qUnit:运行测试子集?

javascript - 在没有服务器端脚本的情况下在 javascript 中绕过同源策略

ruby-on-rails - Mock Sequel 连接到 Oracle 数据库

unit-testing - 单元测试 elixir 功能正常

javascript - sinon.stub() vs sinon.sandbox.stub()?

javascript - QUnit、Sinon.js 和 Backbone 单元测试受挫 : sinon spy appears to fail to detect Backbone Model event callbacks