Javascript 我如何阅读伊斯坦布尔覆盖率报告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26618243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:08:00  来源:igfitidea点击:

How do I read an Istanbul Coverage Report?

javascripttestingjasminecode-coverageistanbul

提问by Scott Sword

I've always used Jasmine for my unit tests, but recently I started using Istanbul to give me code coverage reports. I mean I get the gistof what they are trying to tell me, but I don't really know what each of these percentages represent (Stmts, Branches, Funcs, Lines). So far Googling I have been unable to find a solid explanation/resource.

我一直使用 Jasmine 进行单元测试,但最近我开始使用伊斯坦布尔来提供代码覆盖率报告。我的意思是我明白他们试图告诉我的要点,但我真的不知道这些百分比中的每一个代表什么(Stmts、Branches、Funcs、Lines)。到目前为止,谷歌搜索我一直无法找到可靠的解释/资源。

Question: Like I said I get the gist of it, but can someone post either a proper explanation or a link to a proper explanation?

问题:就像我说的我明白了它的要点,但是有人可以发布正确的解释或指向正确解释的链接吗?

Tertiary Question: Is there any way to identify what specific parts of your code aren't covered? So far without really grokking this report I'm basically guessing.

第三个问题:有什么方法可以确定代码的哪些特定部分没有被涵盖?到目前为止,我还没有真正理解这份报告,我基本上是在猜测。

-------------------|-----------|-----------|-----------|-----------|
File               |   % Stmts |% Branches |   % Funcs |   % Lines |
-------------------|-----------|-----------|-----------|-----------|
   controllers/    |      88.1 |     77.78 |     78.57 |      88.1 |
      dashboard.js |      88.1 |     77.78 |     78.57 |      88.1 |
-------------------|-----------|-----------|-----------|-----------|
All files          |      88.1 |     77.78 |     78.57 |      88.1 |
-------------------|-----------|-----------|-----------|-----------|

回答by Amy Pellegrini

There are a number of coverage criteria, the main ones being:

有许多覆盖标准,主要是:

  • Function coverageHas each function (or subroutine) in the program been called?
  • Statement coverageHas each statement in the program been executed?
  • Branch coverageHas each branch (also called DD-path) of each control structure (such as in if and case statements) been executed? For example, given an if statement, have both the true and false branches been executed? Another way of saying this is, has every edge in the program been executed?
  • Line coveragehas each executable line in the source file been executed?
  • 函数覆盖程序中的每个函数(或子程序)都被调用了吗?
  • 语句覆盖程序中的每条语句都被执行了吗?
  • 分支覆盖每个控制结构(例如在 if 和 case 语句中)的每个分支(也称为 DD-path)是否已执行?例如,给定一个 if 语句,是否同时执行了 true 和 false 分支?另一种说法是,程序中的每条边都被执行了吗?
  • 行覆盖是否已执行源文件中的每个可执行行?

For each case, the percentage represents executed codevs not-executed code, which equals each fraction in percent format (e.g: 50% branches, 1/2).

对于每种情况,百分比表示已执行代码未执行代码,这等于百分比格式中的每个分数(例如:50% 分支,1/2)。

In the file report:

在文件报告中:

  • 'E'stands for 'else path not taken', which means that for the marked if/else statement, the 'if' path has been tested but not the 'else'.
  • 'I'stands for 'if path not taken', which is the opposite case: the 'if' hasn't been tested.
  • The xNin left column is the amount of times that line has been executed.
  • Not executed lines, or pieces of code, will be highlighted in red.
  • 'E'代表'else path not used',这意味着对于标记的if/else语句,'if'路径已经过测试,但没有'else'。
  • 'I'代表“如果没有采用路径”,这是相反的情况:“如果”尚未经过测试。
  • xN左列是线已被执行的次数的量。
  • 未执行的行或代码段将以红色突出显示。

This has been verified for Istanbul v0.4.0, I'm not sure if this still applies for subsequent versions, but being that library is based on solid theoretic principles, behavior shouldn't change too much for newer versions.

这已经针对伊斯坦布尔 v0.4.0 进行了验证,我不确定这是否仍然适用于后续版本,但由于该库基于可靠的理论原则,因此新版本的行为不应有太大变化。

It also provides some color codes -

它还提供了一些颜色代码——

Pink: statements not covered.

粉红色:未涵盖的语句。

Orange: functions not covered.

橙色:未涵盖的功能。

Yellow: branches not covered.

黄色:没有覆盖的树枝。

Full Istanbul docs here:

完整的伊斯坦布尔文档在这里:

https://istanbul.js.org

https://istanbul.js.org

For more in-depth theory on code coverage:

有关代码覆盖率的更深入的理论:

https://en.wikipedia.org/wiki/Code_coverage

https://en.wikipedia.org/wiki/Code_coverage

Hope it helps!

希望能帮助到你!

回答by Yaron Schwimmer

Running istanbul should also produce an HTML file for the report (should be in the coverage folder). This HTML should give you drill-down information when you click on files/folders.

运行 istanbul 还应该为报告生成一个 HTML 文件(应该在coverage文件夹中)。当您单击文件/文件夹时,此 HTML 应为您提供深入信息。

The percentage of functions covered is calculated by the number of functions that were called during tests, divided by total number of functions. Same goes for lines and statements (which will usually be close to each other unless you have very long statements). Branches mean decision points like if-elseblocks. For example, say your code only contains one if-elsestatement, and your tests only pass through the ifpart but not the elsepart, then your branches percentage should be 50%.

覆盖函数的百分比通过测试期间调用的函数数除以函数总数计算得出。行和语句也是如此(除非你有很长的语句,否则它们通常会彼此接近)。分支意味着像if-else块一样的决策点。例如,假设您的代码仅包含一条if-else语句,并且您的测试仅通过if部分而不通过else部分,那么您的分支百分比应为 50%。

Hope that makes things clearer.

希望这能让事情更清楚。

回答by Samuel Pinheiro

Adding to the previous answers

添加到以前的答案

The %Statements is calculated by taking a percentage of the number of statements covered by your test e.g 12/18 * 100 = 66.67%. This means that your test covered only 66.67%.

%Statements 的计算方法是取测试涵盖的语句数的百分比,例如 12/18 * 100 = 66.67%。这意味着您的测试仅覆盖了 66.67%。

The %Branch is also calculated in the same way. Same for your %Functions and %lines.

%Branch 也以同样的方式计算。您的 %Functions 和 %lines 也是如此。

In your project root directory, there is a coverage folder that contains HTML output of your test. Click on it and view it in the browser. You should see something like this

在您的项目根目录中,有一个覆盖文件夹,其中包含您的测试的 HTML 输出。单击它并在浏览器中查看它。你应该看到这样的东西

Image showing the output of your test results

显示测试结果输出的图像

I hope this helps you understand it better.

我希望这可以帮助您更好地理解它。