Wiki source code of Test Coverage
Last modified by Vincent Massol on 2020/10/16 10:05
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{box cssClass="floatinginfobox" title="**Contents**"}} | ||
2 | {{toc/}} | ||
3 | {{/box}} | ||
4 | |||
5 | {{info}} | ||
6 | We now have a [[SonarQube instance>>http://sonar.xwiki.org]] showing Test coverage for both unit tests and integration tests. However it doesn't aggregate coverage data across top level modules (commons, rendering, platform, enterprise, etc). | ||
7 | {{/info}} | ||
8 | |||
9 | We support both [[Jacoco>>http://www.eclemma.org/jacoco/]] and [[Clover>>http://www.atlassian.com/software/clover/overview]] to generate test coverage reports. | ||
10 | |||
11 | To generate test coverage reports make sure you can [[build>>Community.Building]] your module and then pick one of the following strategies below depending on what you wish to generate. | ||
12 | |||
13 | = Single Maven Reactor = | ||
14 | |||
15 | == Using Jacoco == | ||
16 | |||
17 | * Go in the first top level module (e.g. ##xwiki-commons##) and run: {{code}}mvn clean jacoco:prepare-agent install -Djacoco.destFile=/tmp/jacoco.exec -Djacoco.append=false -Plegacy,integration-tests -Dxwiki.revapi.skip=true -Dmaven.test.failure.ignore=true{{/code}} | ||
18 | * Go in all the other top level modules you wish to add and run: {{code}}mvn clean jacoco:prepare-agent install -Djacoco.destFile=/tmp/jacoco.exec -Djacoco.append=true -Plegacy,integration-tests -Dxwiki.revapi.skip=true -Dmaven.test.failure.ignore=true{{/code}} | ||
19 | * Then whenever you wish to generate the full test report, run: {{code}}mvn jacoco:report -Djacoco.dataFile=/tmp/jacoco.exec{{/code}}((( | ||
20 | {{todo}} | ||
21 | Jacoco supports generating report from a single module, it even supports generating aggregated report from several modules using the report-aggregate mojo introduced in 0.7.7 (but note that I don't know if that includes coverage induced by module B on module A). However jacoco doesn't seem to support the ability to execute several maven reactors (for example for building code located in various github repositories), using the same jacoco exec file and then generate a report for that. See also https://groups.google.com/forum/#!topic/jacoco/odVzr7P5i6w | ||
22 | {{/todo}} | ||
23 | ))) | ||
24 | |||
25 | == Using Clover == | ||
26 | |||
27 | Go to the top level module containing children modules for which to generate a Clover report. Note that you won't be able to aggregate Clover data across different Maven runs with this strategy so you really need a single parent module. | ||
28 | |||
29 | 1. Run the following command (adjust the local repo path):((( | ||
30 | {{code language="none"}} | ||
31 | mvn clean clover:setup install clover:aggregate clover:clover -Pclover,integration-tests,dev,jetty,hsqldb -Dxwiki.revapi.skip=true -Dmaven.repo.local=/Users/vmassol/.m2/repository-clover -Dmaven.test.failure.ignore=true -nsu | ||
32 | {{/code}} | ||
33 | |||
34 | {{info}} | ||
35 | * You might need to run the "install" goal instead of the "test" one if your local Maven repository doesn't already contain some test jars (apparently and for some reason Maven won't download them from the remote repository under some conditions). | ||
36 | * Note that we use ##-Dmaven.repo.local## to use a different Maven local repository so that instrumented source code and built binaries don't find their way in your standard local repository (which you could then deploy by mistake, or that'll simply fail your tests later on when you don't run with the Clover profile since instrumented artifacts require the Clover JAR to be present in the classpath at runtime...). | ||
37 | {{/info}} | ||
38 | ))) | ||
39 | |||
40 | = Multiple Maven Reactors = | ||
41 | |||
42 | == Using Jacoco == | ||
43 | |||
44 | {{todo/}} | ||
45 | |||
46 | == Using Clover == | ||
47 | |||
48 | Use a single Clover database to which you add coverage information as you build modules one after another. This strategy is especially useful when you wish to manually run some modules and ensure that coverage data aggregate in a single place so that when you generate the report you have the result of all your runs. | ||
49 | |||
50 | 1. Instrument the source code with Clover for all modules that you want to include in the report, using (adjust the paths):((( | ||
51 | {{code language="none"}} | ||
52 | mvn clover:setup install -Pclover,integration-tests,dev,jetty,hsqldb -Dmaven.clover.cloverDatabase=/Users/vmassol/.xwiki/clover/clover.db -Dxwiki.revapi.skip=true -Dmaven.repo.local=/Users/vmassol/.m2/repository-clover -nsu | ||
53 | {{/code}} | ||
54 | |||
55 | When tests are executed they'll generate coverage data in the specified Clover database. Since there's a single Clover there's no need to merge Clover databases as in strategy 1 above. | ||
56 | ))) | ||
57 | 1. To generate the Clover report, execute the following from any module (adjust path):((( | ||
58 | {{code language="none"}} | ||
59 | mvn clover:clover -N -Dmaven.clover.cloverDatabase=/Users/vmassol/.xwiki/clover/clover.db -Dmaven.repo.local=/Users/vmassol/.m2/repository-clover -nsu | ||
60 | {{/code}} | ||
61 | ))) | ||
62 | 1. Remember to clean your clover database when you're done. | ||
63 | |||
64 | {{info}} | ||
65 | If you don't wish failing tests to stop the generation of the coverage report, you should pass ##-Dmaven.test.failure.ignore=true## on the command line. | ||
66 | {{/info}} | ||
67 | |||
68 | Here are typical steps you'd follow to generate full TPC for XWiki: | ||
69 | |||
70 | * Clean your local repo and remove any previous clover DBs:((( | ||
71 | {{code}} | ||
72 | rm -R ~/.m2/repository-clover/org/xwiki | ||
73 | rm -R ~/.m2/repository-clover/com/xpn | ||
74 | rm -R ~/.xwiki/clover | ||
75 | {{/code}} | ||
76 | ))) | ||
77 | * Generate coverage data for XWiki Commons:((( | ||
78 | {{code}} | ||
79 | cd xwiki-commons | ||
80 | mvn clean -Pclover,integration-tests -Dmaven.repo.local=/Users/vmassol/.m2/repository-clover -nsu | ||
81 | mvn clover:setup install -Pclover,integration-tests -Dmaven.clover.cloverDatabase=/Users/vmassol/.xwiki/clover/clover.db -Dmaven.repo.local=/Users/vmassol/.m2/repository-clover -Dmaven.test.failure.ignore=true -Dxwiki.revapi.skip=true -nsu | ||
82 | {{/code}} | ||
83 | ))) | ||
84 | * Generate Clover report just for Commons:((( | ||
85 | {{code}} | ||
86 | mvn clover:clover -N -Dmaven.clover.cloverDatabase=/Users/vmassol/.xwiki/clover/clover.db -Dmaven.repo.local=/Users/vmassol/.m2/repository-clover -nsu | ||
87 | {{/code}} | ||
88 | ))) | ||
89 | * Generate coverage data for XWiki Rendering:((( | ||
90 | {{code}} | ||
91 | cd xwiki-rendering | ||
92 | mvn clean -Pclover,integration-tests -Dmaven.repo.local=/Users/vmassol/.m2/repository-clover -nsu | ||
93 | mvn clover:setup install -Pclover,integration-tests -Dmaven.clover.cloverDatabase=/Users/vmassol/.xwiki/clover/clover.db -Dmaven.repo.local=/Users/vmassol/.m2/repository-clover -Dmaven.test.failure.ignore=true -Dxwiki.revapi.skip=true -nsu | ||
94 | {{/code}} | ||
95 | ))) | ||
96 | * Generate Clover report for Commons and Rendering:((( | ||
97 | {{code}} | ||
98 | mvn clover:clover -N -Dmaven.clover.cloverDatabase=/Users/vmassol/.xwiki/clover/clover.db -Dmaven.repo.local=/Users/vmassol/.m2/repository-clover -nsu | ||
99 | {{/code}} | ||
100 | ))) | ||
101 | * Generate coverage data for XWiki Platform:((( | ||
102 | {{code}} | ||
103 | cd xwiki-platform | ||
104 | mvn clean -Pclover,integration-tests -Dmaven.repo.local=/Users/vmassol/.m2/repository-clover -nsu | ||
105 | mvn clover:setup install -Pclover,integration-tests -Dmaven.clover.cloverDatabase=/Users/vmassol/.xwiki/clover/clover.db -Dmaven.repo.local=/Users/vmassol/.m2/repository-clover -Dmaven.test.failure.ignore=true -Dxwiki.revapi.skip=true -nsu | ||
106 | {{/code}} | ||
107 | ))) | ||
108 | * Generate full Clover report (for Commons, Rendering and Platform):((( | ||
109 | {{code}} | ||
110 | mvn clover:clover -N -Dmaven.clover.cloverDatabase=/Users/vmassol/.xwiki/clover/clover.db -Dmaven.repo.local=/Users/vmassol/.m2/repository-clover -nsu | ||
111 | {{/code}} | ||
112 | ))) | ||
113 | |||
114 | == Using Clover + Jenkins == | ||
115 | |||
116 | * Install Jenkins LTS and the following plugins: | ||
117 | ** Pipeline plugin | ||
118 | ** Docker Cloud Plugin | ||
119 | * Setup Jenkins to spawn Docker agents using the [[XWiki Build image>>https://github.com/xwiki/xwiki-docker-build/tree/master/build]]. | ||
120 | * Setup a Global Pipeline Library in Jenkins that points to https://github.com/xwiki/xwiki-jenkins-pipeline (make sure it's loaded implicitly). | ||
121 | * Setup a Jenkins Clover pipeline job with a script such as:((( | ||
122 | {{code language='groovy'}} | ||
123 | node('docker') { | ||
124 | xwikiClover([ | ||
125 | [baseline: "20171222-1835", fail: false], | ||
126 | [baseline: "20190106-0207", fail: false], | ||
127 | [baseline: "latest", fail: true] | ||
128 | ]) | ||
129 | } | ||
130 | {{/code}} | ||
131 | ))) | ||
132 | * You can check the {{scm project="xwiki-jenkins-pipeline" path="vars/xwikiClover.groovy"}}code for the ##xwikiClover## step{{/scm}}. | ||
133 | * Check the [[results>>http://maven.xwiki.org/site/clover/]]. | ||
134 | |||
135 | Note that the Clover pipeline script will generate a report with differences from the previous passing report showing the contributions (positive and negative) of each module to the global TPC. | ||
136 | |||
137 | = Example Reports = | ||
138 | |||
139 | * [[All Reports>>http://maven.xwiki.org/site/clover/]] | ||
140 | * [[XWiki Commons/Rendering (2nd December 2016)>>http://maven.xwiki.org/site/clover/20161202/]] | ||
141 | * [[XWiki Commons/Rendering (21st October 2015)>>http://maven.xwiki.org/site/clover/20151021/]] | ||
142 | * [[XWiki Commons/Rendering/Platform/Enterprise (1st of July 2012)>>http://maven.xwiki.org/site/clover/20120701/]] | ||
143 | * [[XWiki Commons (12 March 2012)>>http://maven.xwiki.org/site/clover/20120312/xwiki-commons/]], [[XWiki Rendering (12 March 2012)>>http://maven.xwiki.org/site/clover/20120312/xwiki-rendering/]] | ||
144 | * [[XWiki Enterprise + Platform (17 October 2010)>>http://maven.xwiki.org/site/clover/20101017/]] (includes functional test coverage) | ||
145 | * [[XWiki Rendering (12 October 2010)>>attach:xwiki-rendering-clover-20101012.zip]] | ||
146 | * [[XWiki Rendering (4 May 2009)>>attach:xwiki-rendering-clover-20090504.zip]] | ||
147 | * [[XWiki Core (12 August 2008)>>attach:xwiki-core-clover-20080812.zip]] |