go tool cover的HTML输出:按文件显示的覆盖率概览

huangapple go评论91阅读模式
英文:

HTML output of go tool cover: Overview of coverage per file

问题

go tool提供了一个漂亮的HTML视图,可以突出显示未覆盖的代码。

然而,在选择框中选择要显示的文件时,你无法知道其中是否有任何未覆盖的代码。

如果我有很多文件和大量的覆盖率,我经常会一个接一个地检查文件,以找到没有100%覆盖率的文件。

我知道-func开关,但是否有一种方法可以过滤HTML输出,只显示没有100%覆盖率的文件,或者在HTML视图中显示文件的覆盖率百分比的概览?

英文:

The go tool offers a nice html view highlighting uncovered code.

However you need to choose the file to show in the select box without knowing if there is any non covered code in it.

If I have a large coverage and lots of files I often find myself checking one file after another in order to find the files not having 100% coverage.

I am aware of the -func switch, but is there a way to either filter the html output to just the files not having 100% coverage or to get an overview inside the html view showing the percentage of coverage next to the file?

答案1

得分: 5

我刚刚发送了一个更改,以添加此功能到封面工具:https://codereview.appspot.com/127030043

英文:

I've just sent a change to add this feature to the cover tool: https://codereview.appspot.com/127030043

答案2

得分: 2

通过阅读源代码,似乎没有像你描述的那样的选项。但这让我想知道,你认为100%覆盖率的文件是什么样的?HTML工具使用0-10的范围来着色行,其中0表示完全未覆盖,10表示覆盖非常全面。那么,如何表示覆盖率为1或2的行呢?我的意思是,它有一定的覆盖率,但肯定不多(尤其对于关键内容)。

在文件旁边的下拉菜单中添加一个显示非0值覆盖行百分比的选项可能会很有趣。这并不难实现,但意味着htmlGen需要一个结构体来包装缓冲区和百分比计数器,或者需要传入一个额外的指针来跟踪覆盖行的百分比。其余部分都很容易,只需在templateFile类型中添加一个PercentCovered,并修改模板HTML以在下拉菜单中包含它。

总的来说,除非这纯粹是为了尝试在测试中实现“完全代码覆盖”,否则我建议不要过分强调这个统计数据,因为它可能会对测试的质量或完整性产生误导。

编辑
想一想,你可能可以使用浏览器脚本注入插件(如TamperMonkey或GreaseMonkey)自己实现这个功能(分别适用于Chrome和Firefox)。一点相当直接的JavaScript代码应该可以给出相当不错的结果。事实证明,颜色的类别不是按行定义的,而是按照相似覆盖行的组定义的,所以你的覆盖行百分比可能会有些棘手,因为你需要计算每个span块中的行数(以“\n”为分隔符),但同样,这并不难。我认为这是一个值得尝试的想法,如果有时间的话,我可能会尝试自己实现它。

编辑2
以下是相关的JavaScript代码(依赖于jQuery,因为我不懂JavaScript)...如果你将这段代码放在生成的文件末尾(在闭合的</html>标签之前),它将为每个文件的下拉菜单添加一个非零覆盖率的百分比。它还考虑了未跟踪的行。

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  <script>
    $(document).ready(function() {
        $('#files').find("option").each(function(i, el) {
          var id = $(this).val();
          var div = $('#' + id);
          var lines = div.html().split("\n").length;
          var covered = 0;
          var not_tracked = lines;
          div.find('span').each(function(i, el) {
            var span_lines = $(this).html().split("\n").length;
            var klass = $(this).attr('class');
            var coverage_type = parseInt(klass.replace("cov", ""));
            if (coverage_type != 0) {
              covered += span_lines;
            }
            not_tracked -= span_lines
          });
          lines -= not_tracked
          $(this).text((covered*100/lines).toFixed(2) + "%" + " " + $(this).text());
        });
    });
  </script>

我无法让TamperMonkey在file://协议下工作(我认为它不支持该协议),但GreaseMonkey可以通过一些设置更改来使用。(图片已模糊处理以保护隐私。)

英文:

Just reading through the source there does not appear to be any sort of option like you describe. But it makes me wonder, what would you consider a file with 100% coverage? The html tool colors lines using a range of 0-10 where 0 is not covered at all and 10 has extensive coverage. So how would a line with a 1 or 2 be represented? I mean, it has some coverage, but certainly not very much (especially for something that is critical.)

It could be interesting to add a percentage in the drop down next to the files that shows a percentage of lines covered with a non-0 value. This wouldn't even be very hard to add, but would mean that htmlGen would need either a struct to wrap the buffer and counter for percentages or an additional pointer would need to be passed in to track the percentage of covered lines. The rest would be pretty easy, adding a PercentCovered to templateFile type, and modifying the template html to include it in the drop down.

All in all, unless this is purely an exercise for yourself in trying to achieve "full code coverage" in tests, I would warn against putting too much emphasis on this stat as it can be entirely misleading as to the quality or completeness of ones tests.

EDIT
Come to think of it, you could probably do this yourself using a browser script injection plugin like TamperMonkey or GreaseMonkey (chrome and ff respectively.) I little bit of fairly straight forward javascript should give you pretty good results. As it turns out the classes for the colors are not defined per line, but per group of similarly covered lines, so your percentage of lines covered might be a little tricker as you would need to count the lines within each span block (splitting on "\n"), but again, it wouldn't be all that hard. I think this is a worthwhile idea and might try implementing it myself if I find the time.

EDIT 2
Here is the relevant javascript (relies on jquery because I don't do JS)... If you stick this in the end of the generated file (before the closing </html> tag it will add a percentage to the drop down per file for non-zero coverage. It also takes into account lines that are not tracked.

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  <script>
    $(document).ready(function() {
        $('#files').find("option").each(function(i, el) {
          var id = $(this).val();
          var div = $('#' + id);
          var lines = div.html().split("\n").length;
          var covered = 0;
          var not_tracked = lines;
          div.find('span').each(function(i, el) {
            var span_lines = $(this).html().split("\n").length;
            var klass = $(this).attr('class');
            var coverage_type = parseInt(klass.replace("cov", ""));
            if (coverage_type != 0) {
              covered += span_lines;
            }
            not_tracked -= span_lines
          });
          lines -= not_tracked
          $(this).text((covered*100/lines).toFixed(2) + "%" + " " + $(this).text());
        });
    });
  </script>

I couldn't get TamperMonkey to work with file:// protocol (I don't think it supports it, but GreaseMonkey does with some setting changes.)
go tool cover的HTML输出:按文件显示的覆盖率概览

The screenshot has been blurred to protect the innocent.

huangapple
  • 本文由 发表于 2014年8月11日 00:41:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/25230810.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定