duanshan2988 2014-08-10 16:41
浏览 32
已采纳

Go工具封面的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?

  • 写回答

2条回答 默认 最新

  • douqin1932 2014-08-10 20:49
    关注

    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 " "), 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("
    ").length;
              var covered = 0;
              var not_tracked = lines;
              div.find('span').each(function(i, el) {
                var span_lines = $(this).html().split("
    ").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.) Modified image of profile

    The screenshot has been blurred to protect the innocent.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试