项目作者: guozheng

项目描述 :
a quick script to parse jacoco html report to generate code coverage numbers
高级语言: HTML
项目地址: git://github.com/guozheng/jacoco-parser.git
创建时间: 2019-11-03T07:15:01Z
项目社区:https://github.com/guozheng/jacoco-parser

开源协议:MIT License

下载


jacoco-parser

A quick script to parse jacoco html report to generate code coverage numbers. Shamelessly borrowed the idea from tavvasubbareddy’s JacocoUtils in Java.

Jacoco Coverage Report

index.html is a test coverage report file from Jacoco’s own coverage report. It is a single line html file that contains coverage summary. The summary data looks like this (pretty printed):

  1. ...
  2. <thead>
  3. <tr>
  4. <td class="sortable" id="a" onclick="toggleSort(this)">Element</td>
  5. <td class="down sortable bar" id="b" onclick="toggleSort(this)">Missed Instructions</td>
  6. <td class="sortable ctr2" id="c" onclick="toggleSort(this)">Cov.</td>
  7. <td class="sortable bar" id="d" onclick="toggleSort(this)">Missed Branches</td>
  8. <td class="sortable ctr2" id="e" onclick="toggleSort(this)">Cov.</td>
  9. <td class="sortable ctr1" id="f" onclick="toggleSort(this)">Missed</td>
  10. <td class="sortable ctr2" id="g" onclick="toggleSort(this)">Cxty</td>
  11. <td class="sortable ctr1" id="h" onclick="toggleSort(this)">Missed</td>
  12. <td class="sortable ctr2" id="i" onclick="toggleSort(this)">Lines</td>
  13. <td class="sortable ctr1" id="j" onclick="toggleSort(this)">Missed</td>
  14. <td class="sortable ctr2" id="k" onclick="toggleSort(this)">Methods</td>
  15. <td class="sortable ctr1" id="l" onclick="toggleSort(this)">Missed</td>
  16. <td class="sortable ctr2" id="m" onclick="toggleSort(this)">Classes</td>
  17. </tr>
  18. </thead>
  19. <tfoot>
  20. <tr>
  21. <td>Total</td>
  22. <td class="bar">1,323 of 26,881</td>
  23. <td class="ctr2">95%</td>
  24. <td class="bar">141 of 2,074</td>
  25. <td class="ctr2">93%</td>
  26. <td class="ctr1">209</td>
  27. <td class="ctr2">2,558</td>
  28. <td class="ctr1">328</td>
  29. <td class="ctr2">6,228</td>
  30. <td class="ctr1">77</td>
  31. <td class="ctr2">1,489</td>
  32. <td class="ctr1">15</td>
  33. <td class="ctr2">289</td>
  34. </tr>
  35. </tfoot>
  36. ...

The script then uses regex to find those <td> element values and return a dictionary structure using counter name as key and a tuple (missed, total, coverage_pct) as value.

Usage

```shell script

python jacoco_parser/parser.py path_to_index.html [path_to_old_index.html]
```

Parse and print one coverage report data

```shell script

python jacoco_parser.parser.py index.html
counter: METHODS - missed: 77, total: 1,489, coverage: 94.8287441236%
counter: CXTY - missed: 209, total: 2,558, coverage: 91.8295543393%
counter: INSTRUCTION - missed: 1,323, total: 26,881, coverage: 95.0783080987%
counter: LINES - missed: 328, total: 6,228, coverage: 94.7334617855%
counter: CLASSES - missed: 15, total: 289, coverage: 94.8096885813%
counter: BRANCH - missed: 141, total: 2,074, coverage: 93.2015429122%
```

Compare and print two coverage reports difference

```shell script

python jacoco_parser.parser.py index.html index_old.html
counter: METHODS - missed: 77, total: 1,489, coverage: 94.8287441236%, change: 1.25684220177%
counter: CXTY - missed: 209, total: 2,558, coverage: 91.8295543393%, change: 0.132672540923%
counter: INSTRUCTION - missed: 1,323, total: 26,881, coverage: 95.0783080987%, change: 1.73620956318%
counter: LINES - missed: 328, total: 6,228, coverage: 94.7334617855%, change: 0.303244115831%
counter: CLASSES - missed: 15, total: 289, coverage: 94.8096885813%, change: 0.974072142959%
counter: BRANCH - missed: 141, total: 2,074, coverage: 93.2015429122%, change: 2.19400995556%
```

Methods

get_stats returns the coverage data summary in a dictionary:

  1. {'METHODS': ('77', '1,489', 94.82874412357287), 'CXTY': ('209', '2,558', 91.8295543393276), 'INSTRUCTION': ('1,323', '26,881', 95.07830809865705), 'LINES': ('328', '6,228', 94.7334617854849), 'CLASSES': ('15', '289', 94.80968858131487), 'BRANCH': ('141', '2,074', 93.20154291224686)}

diff_stats returns the coverage data difference in a dictionary.