代码覆盖 - 消除误报

时间:2013-07-10 15:43:18

标签: javascript unit-testing code-coverage jasmine gruntjs

我正在使用istanbul(通过grunt,jasmine和phantomJS)为我的单元测试生成代码覆盖率报告。我得到了我称之为假阳性的东西,这个假阳性是由我正在测试的模块作为依赖调用其方法的模块引起的,即

module1.jsmodule2.js作为依赖项加载。 module1的单元测试调用module1.method,然后调用module2.method。当我查看代码覆盖率报告时module2.method以绿色突出显示,即使没有对其进行测试。

这是我应该被忽略的事情,我该如何避免呢?

2 个答案:

答案 0 :(得分:2)

我创建了这个grunt任务来帮助识别他们自己的测试套件未明确涵盖的文件

grunt.registerTask('missingSpecs', 'Missing specs list', function (env, limit) {
    var specs = getFileList(getSpecs(env)),
        src = getFileList(getSrc(env)),
        missingSpecs = [];

    for (var file in src) {
        if (specs.indexOf(src[file]) === -1) {
            missingSpecs.push(src[file]);
        }
    }
    if (missingSpecs.length) {
        console.log(
            '\n******************************************************************************************************\n' +
            '*** The following common js files aren\'t covered by any tests. They won\'t write themselves, y\'know ***\n' +
            '******************************************************************************************************\n'
            );
        missingSpecs.sort(function () {
            return Math.random() - 0.5;
        }).slice(0,limit).forEach(function (file) {
            console.log(file);
        });
    }

})

其中getFileList使用grunt.file.expand和一些正则表达式获取模块名称列表。

答案 1 :(得分:0)

我也注意到了这一点,但我认为这是正确的行为。代码覆盖仅表示代码在测试期间执行。并不是代码是断言的一部分。

相关问题