2014-08-10 16:41



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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • douqin1932 douqin1932 7年前

    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.

    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>
        $(document).ready(function() {
            $('#files').find("option").each(function(i, el) {
              var id = $(this).val();
              var div = $('#' + id);
              var lines = div.html().split("
              var covered = 0;
              var not_tracked = lines;
              div.find('span').each(function(i, el) {
                var span_lines = $(this).html().split("
                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());

    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.

    点赞 评论 复制链接分享
  • douji3426 douji3426 7年前

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

    点赞 评论 复制链接分享