简体   繁体   中英

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.

I have two files, the first one (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):

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:

{
  "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?

I also tried to use nodes require instead of jscs, without success.

EDIT: I just tried mocha -R json-cov test.js --coverage which results in an error if I use jscs require . When I use nodes require the result is the same as above.

EDIT: I can't even run jscoverage from console. I created a folder foo and foo-cov and copied my foo.js in the folder foo. Then I ran jscoverage foo foo-cov which gave me an error 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?

EDIT: If it is of any relevance, I am using Windows.

EDIT: Just realized that there isn't only a single 'jscoverage' package available via npm, but also a 'visionmedia-jscoverage'. Trying to install that one fails. Probably because of Windows vs. 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 . 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

Changing this to td.source!= line.source fixes this last issue I had.

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

jscoverage --no-highlight foo foo-cov

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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