-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
jacocoPlugin.xml
288 lines (280 loc) · 11.6 KB
/
jacocoPlugin.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<!--
~ Copyright 2013 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<chapter id="jacoco_plugin">
<title>The JaCoCo Plugin</title>
<note>
<para>
The JaCoCo plugin is currently <link linkend="feature_lifecycle">incubating</link>. Please be aware that the DSL and other configuration may change in later Gradle versions.
</para>
</note>
<para>The JaCoCo plugin provides code coverage metrics for Java code via integration with <ulink url="http://www.eclemma.org/jacoco/">JaCoCo</ulink>.
</para>
<section id="sec:jacoco_getting_started">
<title>Getting Started</title>
<para>To get started, apply the JaCoCo plugin to the project you want to calculate code coverage for.</para>
<sample id="applyJacoco" dir="testing/jacoco/quickstart" title="Applying the JaCoCo plugin">
<sourcefile file="build.gradle" snippet="apply-plugin"/>
</sample>
<para>
If the Java plugin is also applied to your project, a new task named
<literal>jacocoTestReport</literal>
is created that depends on the
<literal>test</literal>
task.
The report is available at
<filename><replaceable>$buildDir</replaceable>/reports/jacoco/test</filename>. By default, a HTML report is generated.
</para>
</section>
<section id="sec:configuring_the_jacoco_plugin">
<title>Configuring the JaCoCo Plugin</title>
<para>
The JaCoCo plugin adds a project extension named <literal>jacoco</literal> of type <apilink class="org.gradle.testing.jacoco.plugins.JacocoPluginExtension"/>,
which allows configuring defaults for JaCoCo usage in your build.
</para>
<sample id="configJacoco" dir="testing/jacoco/quickstart" title="Configuring JaCoCo plugin settings">
<sourcefile file="build.gradle" snippet="jacoco-configuration"/>
</sample>
<table>
<title>Gradle defaults for JaCoCo properties</title>
<thead>
<tr>
<td>Property</td>
<td>Gradle default</td>
</tr>
</thead>
<tr>
<td>reportsDir</td>
<td>“<replaceable>$buildDir</replaceable>/reports/jacoco”
</td>
</tr>
</table>
</section>
<section id="sec:jacoco_report_configuration">
<title>JaCoCo Report configuration</title>
<para>The
<apilink class="org.gradle.testing.jacoco.tasks.JacocoReport"/>
task can be used to generate code coverage reports in different formats.
It implements the standard Gradle type <apilink class="org.gradle.api.reporting.Reporting"/> and exposes a report container of
type <apilink class="org.gradle.testing.jacoco.tasks.JacocoReportsContainer" />.
</para>
<sample id="configJacocoReport" dir="testing/jacoco/quickstart" title="Configuring test task">
<sourcefile file="build.gradle" snippet="report-configuration"/>
</sample>
<imageobject>
<imagedata fileref="img/jacocoHtmlReport.png" width="903px" depth="277px"/>
</imageobject>
</section>
<section id="sec:jacoco_report_violation_rules">
<title>Enforcing code coverage metrics</title>
<note>
<para>
This feature requires the use of JaCoCo version 0.6.3 or higher.
</para>
</note>
<para>
The <apilink class="org.gradle.testing.jacoco.tasks.JacocoCheck"/> task can be used to verify if code coverage
metrics are met based on configured rules. Its API exposes two methods,
<apilink class="org.gradle.testing.jacoco.tasks.JacocoCheck" method="violationRules(groovy.lang.Closure)"/> and
<apilink class="org.gradle.testing.jacoco.tasks.JacocoCheck" method="violationRules(org.gradle.api.Action)"/>,
the main entry point for configuring rules.
Invoking any of those methods returns an instance of <apilink class="org.gradle.testing.jacoco.tasks.rules.JacocoViolationRulesContainer"/> providing
extensive configuration options. The build fails if any of the configured rules are not met. JaCoCo only reports the first violated rule.
</para>
<para>
Code coverage requirements can be specified for a project as a whole, for individual files, and for
particular JaCoCo-specific types of coverage, e.g., lines covered or branches covered. The following example
describes the syntax.
</para>
<sample id="configViolationRules" dir="testing/jacoco/quickstart" title="Configuring violation rules">
<sourcefile file="build.gradle" snippet="violation-rules-configuration"/>
</sample>
</section>
<section id="sec:jacoco_specific_task_configuration">
<title>JaCoCo specific task configuration</title>
<para>The JaCoCo plugin adds a
<apilink class="org.gradle.testing.jacoco.plugins.JacocoTaskExtension"/>
extension to all tasks of type
<apilink class="org.gradle.api.tasks.testing.Test"/>.
This extension allows the configuration of the JaCoCo specific properties of the test task.
</para>
<sample id="jacocotesttast" dir="testing/jacoco/quickstart" title="Configuring test task">
<sourcefile file="build.gradle" snippet="testtask-configuration"/>
</sample>
<table>
<title>Default values of the JaCoCo Task extension</title>
<thead>
<tr>
<td>Property</td>
<td>Gradle default</td>
</tr>
</thead>
<tr>
<td>enabled</td>
<td>true</td>
</tr>
<tr>
<td>destPath</td>
<td><replaceable>$buildDir</replaceable>/jacoco
</td>
</tr>
<tr>
<td>append</td>
<td>true</td>
</tr>
<tr>
<td>includes</td>
<td>[]</td>
</tr>
<tr>
<td>excludes</td>
<td>[]</td>
</tr>
<tr>
<td>excludeClassLoaders</td>
<td>[]</td>
</tr>
<tr>
<td>includeNoLocationClasses</td>
<td>false</td>
</tr>
<tr>
<td>sessionId</td>
<td>
<literal>auto-generated</literal>
</td>
</tr>
<tr>
<td>dumpOnExit</td>
<td>
<literal>true</literal>
</td>
</tr>
<tr>
<td>output</td>
<td>
<literal>Output.FILE</literal>
</td>
</tr>
<tr>
<td>address</td>
<td>
<literal>-</literal>
</td>
</tr>
<tr>
<td>port</td>
<td>
<literal>-</literal>
</td>
</tr>
<tr>
<td>classDumpPath</td>
<td>
<literal>-</literal>
</td>
</tr>
<tr>
<td>jmx</td>
<td>
<literal>false</literal>
</td>
</tr>
</table>
<para>While all tasks of type
<apilink class="org.gradle.api.tasks.testing.Test"/>
are automatically enhanced to provide coverage information when the <literal>java</literal> plugin has been applied,
any task that implements <apilink class="org.gradle.process.JavaForkOptions"/> can be enhanced by the JaCoCo plugin.
That is, any task that forks Java processes can be used to generate coverage information.
</para>
<para>
For example you can configure your build to generate code coverage using the <literal>application</literal> plugin.
</para>
<sample id="jacoco-application-setup" dir="testing/jacoco/application" includeLocation="true" title="Using application plugin to generate code coverage data">
<sourcefile file="build.gradle" snippet="application-configuration"/>
</sample>
<sample id="jacoco-application-output" dir="testing/jacoco/application" title="Coverage reports generated by applicationCodeCoverageReport">
<layout after='run applicationCodeCoverageReport'>
build/jacoco/run.exec
build/reports/jacoco/applicationCodeCoverageReport/html/index.html
</layout>
</sample>
</section>
<section id="sec:jacoco_tasks">
<title>Tasks</title>
<para>For projects that also apply the Java Plugin, The JaCoCo plugin automatically adds the following tasks:</para>
<table>
<title>JaCoCo plugin - tasks</title>
<thead>
<tr>
<td>Task name</td>
<td>Depends on</td>
<td>Type</td>
<td>Description</td>
</tr>
</thead>
<tr>
<td>
<literal>jacocoTestReport</literal>
</td>
<td>-</td>
<td>
<apilink class="org.gradle.testing.jacoco.tasks.JacocoReport"/>
</td>
<td>Generates code coverage report for the test task.</td>
</tr>
<tr>
<td>
<literal>jacocoTestCheck</literal>
</td>
<td>-</td>
<td>
<apilink class="org.gradle.testing.jacoco.tasks.JacocoCheck"/>
</td>
<td>Verifies code coverage metrics based on specified rules for the test task.</td>
</tr>
</table>
</section>
<section id="sec:jacoco_dependency_management">
<title>Dependency management</title>
<para>The JaCoCo plugin adds the following dependency configurations:</para>
<table>
<title>JaCoCo plugin - dependency configurations</title>
<thead>
<tr>
<td>Name</td>
<td>Meaning</td>
</tr>
</thead>
<tr>
<td>
<filename>jacocoAnt</filename>
</td>
<td>The JaCoCo Ant library used for running the
<literal>JacocoReport</literal>
and
<literal>JacocoMerge</literal>
tasks.
</td>
</tr>
<tr>
<td>
<filename>jacocoAgent</filename>
</td>
<td>The JaCoCo agent library used for instrumenting the code under test.</td>
</tr>
</table>
</section>
</chapter>