简体   繁体   English

单元测试中的代码覆盖率

[英]Code coverage in unit tests

I am trying to get what lines of code my unit tests do not cover. 我试图得到我的单元测试没有涵盖的代码行。

I do my unit tests with mocha, which offers a reporter "json-cov" which should report how many lines I didn't execute. 我用mocha进行单元测试,它提供了一个记者“json-cov”,它应该报告我没有执行多少行。

I have two files, the first one (foo.js): 我有两个文件,第一个(foo.js):

module.exports = function () {
    for (var result = 0, i = 0; i < arguments.length; i++) {
        result += arguments[i];
    }
    return result;
}

and the second one (test.js): 和第二个(test.js):

var expect = require('expect.js'),
    jsc = require('jscoverage'),
    //foo = require('./foo.js');
    foo = jsc.require(module, './foo.js');

describe('foo', function () {
    it('should add all arguments', function () {
        expect(foo(1,1)).to.be(2);
    });
});

When I run mocha -R json-cov test.js I get the following result: 当我运行mocha -R json-cov test.js我得到以下结果:

{
  "instrumentation": "node-jscoverage",
  "sloc": 0,
  "hits": 0,
  "misses": 0,
  "coverage": 0,
  "files": [],
  "stats": {
    "suites": 1,
    "tests": 1,
    "passes": 1,
    "pending": 0,
    "failures": 0,
    "start": "2013-01-30T18:00:15.785Z",
    "end": "2013-01-30T18:00:15.787Z",
    "duration": 2
  },
  "tests": [
    {
      "title": "should add all arguments",
      "fullTitle": "foo should add all arguments",
      "duration": 1
    }
  ],
  "failures": [],
  "passes": [
    {
      "title": "should add all arguments",
      "fullTitle": "foo should add all arguments",
      "duration": 1
    }
  ]
}

What am I doing wrong, so that sloc, hits, misses and coverage are 0? 我做错了什么,所以sloc,hits,miss和coverage都是0?

I also tried to use nodes require instead of jscs, without success. 我也尝试使用节点require而不是jscs,但没有成功。

EDIT: I just tried mocha -R json-cov test.js --coverage which results in an error if I use jscs require . 编辑:我只是尝试了mocha -R json-cov test.js --coverage ,如果我使用jscs require导致错误。 When I use nodes require the result is the same as above. 当我使用节点require的结果是一样的上面。

EDIT: I can't even run jscoverage from console. 编辑:我甚至无法从控制台运行jscoverage。 I created a folder foo and foo-cov and copied my foo.js in the folder foo. 我创建了一个文件夹foo和foo-cov,并将我的foo.js复制到文件夹foo中。 Then I ran jscoverage foo foo-cov which gave me an error abs source path or abs dest path needed! 然后我运行了jscoverage foo foo-cov ,它给了我一个错误的abs source path or abs dest path needed! . I also tried absolute paths and a few other ways to arrange the arguments. 我还尝试了绝对路径和其他一些方法来安排参数。 No success. 没有成功。 How can I prepare the files for jscoverage? 如何为jscoverage准备文件?

EDIT: If it is of any relevance, I am using Windows. 编辑:如果它有任何相关性,我使用的是Windows。

EDIT: Just realized that there isn't only a single 'jscoverage' package available via npm, but also a 'visionmedia-jscoverage'. 编辑:刚刚意识到通过npm不仅有一个'jscoverage'包,而且还有一个'visionmedia-jscoverage'。 Trying to install that one fails. 试图安装那个失败。 Probably because of Windows vs. Linux. 可能是因为Windows与Linux。

EDIT: Got it to work. 编辑:得到它的工作。 Instead of using a node package to prepare the code, I now just run jscoverage.exe (downloaded from here ) from the console, and then go with mocha -R html-cov test.js > coverage.html . 我现在只是从控制台运行jscoverage.exe(从这里下载),然后使用mocha -R html-cov test.js > coverage.html ,而不是使用节点包来准备代码。 Now I have the problem that some code gets escaped. 现在我遇到了一些代码被转义的问题。 So I get 所以我明白了

<span class="k">var</span> foo <span class="k">=</span> <span class="k">{</span>

instead of 代替

var foo = {

with highlighting. 突出显示。

EDIT: The tags got escaped because they were rendered via a jade template with this code: td.source= line.source 编辑:标签被转义,因为它们是通过带有以下代码的玉模板呈现的: td.source= line.source

Changing this to td.source!= line.source fixes this last issue I had. 将此更改为td.source!= line.source修复了我的最后一个问题。

转义代码问题可以通过使用jscoverage的“--no-highlight”选项来编辑mocha的jade模板,如下所示:

jscoverage --no-highlight foo foo-cov

按照我的编辑,看看我做了什么来解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM