OOTB hybris provides ant unittests
command to run unit and integration tests, which are pretty low level and requires additional configuration to simplify run and result collection for DevOps. Below you can find configuration for buildcallbacks.xml
, which you can just copy-paste into any hybris project (version 6.0+) and significantly simplify DevOps job required for setup of CI/CD pipeline.
First of all was added ant ci
, ci-ut
, ci-it
, ci-reports
targets. This commands are wrappers for ant unittests and ant integrationtests targets with such improvements:
- Test runs with JaCoCo(code coverage utility)
ci-ut
runs both regular and web unit tests
ci-ut
runs unit tests without starting of hybris minimal-wrapper server
ci-it
runs integration tests (web integration tests are not run)
ci-report
combines reports of unittests and integration tests in one report
ci
, ci-ut
, ci-it
targets creates junit reports in ${HYBRIS_LOG_DIR}/ci-ut, ${HYBRIS_LOG_DIR}/ci-it or ${HYBRIS_LOG_DIR}/ci directories. Inside this directories will be generated jacoco-report directory with report on coverage
Usage is same as ant unittests. It means that you are able to use “-D” properties to filter tests, for example ant ci -Dtestclasses.extensions=trainingcore
.
Was introduced new run attribute -Dtestclasses.all.custom.extensions=true
for ant ci
, ci-ut
, ci-it
, ci-reports
targets, which will execute tests inside all custom extensions, so there is no more need into manual scan of extensions and usage of -Dtestclasses.extensions=.
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
<!-- CI targets -->
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath>
<fileset dir="${ext.blogcore.path}/lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
</taskdef>
<target name="ci-js" description="Sample target to be override per project">
<foreach target="bar" param="theFile">
<fileset dir="${server.src}" casesensitive="yes">
<include name="**/*.java"/>
<exclude name="**/*Test*"/>
</fileset>
</foreach>
<fileset dir="${HYBRIS_BIN_DIR}/../../">
<include name="package.json"/>
</fileset>
<test_fe_common feBaseDir=""/>
</target>
<target name="ci" description="Generate unit+integration test reports for CI">
<antcall target="ci-ut"/>
<antcall target="ci-it"/>
<antcall target="ci-report"/>
</target>
<target name="ci-ut" description="Generate unit test reports for CI">
<!-- Create temp dir for this target -->
<delete dir="${HYBRIS_LOG_DIR}/ci-ut" failonerror="false"/>
<mkdir dir="${HYBRIS_LOG_DIR}/ci-ut/raw"/>
<!-- Clear report folders -->
<delete dir="${HYBRIS_LOG_DIR}/junit" failonerror="false"/>
<delete dir="${HYBRIS_TEMP_DIR}/junit" failonerror="false"/>
<replace_test_extension_property_if_specified/>
<jacoco:agent property="JACOCO_AGENT_JVM_PROPERTY" destfile="${HYBRIS_LOG_DIR}/ci-ut/jacoco-ut.exec"
append="true"/>
<!-- Run unittests without hybris start -->
<ant dir="${platformhome}" target="unittests" inheritrefs="false">
<property name="testclasses.suppress.junit.tenant" value="true"/>
<property name="standalone.javaoptions" value="${standalone.javaoptions} ${JACOCO_AGENT_JVM_PROPERTY}"/>
</ant>
<!-- Copy test raw results to temp dir-->
<copy todir="${HYBRIS_LOG_DIR}/ci-ut/raw">
<fileset dir="${HYBRIS_TEMP_DIR}/junit"/>
</copy>
<!-- Run WEB unittests without hybris start -->
<ant dir="${platformhome}" target="unittests" inheritrefs="false">
<property name="testclasses.suppress.junit.tenant" value="true"/>
<property name="testclasses.web" value="true"/>
<property name="standalone.javaoptions" value="${standalone.javaoptions} ${JACOCO_AGENT_JVM_PROPERTY}"/>
</ant>
<!-- Copy test raw results to temp dir-->
<copy todir="${HYBRIS_LOG_DIR}/ci-ut/raw">
<fileset dir="${HYBRIS_TEMP_DIR}/junit"/>
</copy>
<!-- Generate JUNIT report -->
<junitreport todir="${HYBRIS_LOG_DIR}/ci-ut">
<fileset dir="${HYBRIS_LOG_DIR}/ci-ut/raw">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${HYBRIS_LOG_DIR}/ci-ut"/>
</junitreport>
<!--Generate JaCoCo coverage-->
<generateCoverageReport name="ut" processFolder="${HYBRIS_LOG_DIR}/ci-ut" processFile="jacoco-ut.exec"/>
<echo message="==========================================================="/>
<echo message="UNIT TEST REPORT DATA: ${HYBRIS_LOG_DIR}/ci-ut/index.html"/>
<echo message="JACOCO COVERAGE REPORT: ${HYBRIS_LOG_DIR}/ci-ut/jacoco-report/index.html"/>
<echo message="==========================================================="/>
</target>
<target name="ci-it" description="Generate integration test reports for CI">
<!-- Create temp dir for this target -->
<delete dir="${HYBRIS_LOG_DIR}/ci-it" failonerror="false"/>
<mkdir dir="${HYBRIS_LOG_DIR}/ci-it/raw"/>
<!-- Clear report folders -->
<delete dir="${HYBRIS_TEMP_DIR}/junit" failonerror="false"/>
<delete dir="${HYBRIS_LOG_DIR}/junit" failonerror="false"/>
<replace_test_extension_property_if_specified/>
<!-- Run integration tests. -->
<!-- All integration tests inside web were moved outside. -->
<jacoco:agent property="JACOCO_AGENT_JVM_PROPERTY" destfile="${HYBRIS_LOG_DIR}/ci-it/jacoco-it.exec"
append="true"/>
<ant dir="${platformhome}" target="integrationtests" inheritrefs="false">
<property name="standalone.javaoptions" value="${standalone.javaoptions} ${JACOCO_AGENT_JVM_PROPERTY}"/>
</ant>
<!-- Copy test raw results to temp dir-->
<copy todir="${HYBRIS_LOG_DIR}/ci-it/raw">
<fileset dir="${HYBRIS_TEMP_DIR}/junit"/>
</copy>
<!-- Generate JUNIT report -->
<junitreport todir="${HYBRIS_LOG_DIR}/ci-it">
<fileset dir="${HYBRIS_LOG_DIR}/ci-it/raw">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${HYBRIS_LOG_DIR}/ci-it"/>
</junitreport>
<!--Generate JaCoCo coverage-->
<generateCoverageReport name="it" processFolder="${HYBRIS_LOG_DIR}/ci-it" processFile="jacoco-it.exec"/>
<echo message="==========================================================="/>
<echo message="INTEGRATION TEST REPORT: ${HYBRIS_LOG_DIR}/ci-it/index.html"/>
<echo message="JACOCO COVERAGE REPORT: ${HYBRIS_LOG_DIR}/ci-it/jacoco-report/index.html"/>
<echo message="==========================================================="/>
</target>
<target name="ci-report" description="Generate unittest and integration test summary report for CI">
<!-- Create temp dir for this target -->
<delete dir="${HYBRIS_LOG_DIR}/ci" failonerror="false"/>
<mkdir dir="${HYBRIS_LOG_DIR}/ci"/>
<!-- Generate summary report -->
<junitreport todir="${HYBRIS_LOG_DIR}/ci">
<fileset dir="${HYBRIS_LOG_DIR}/ci-ut/raw">
<include name="TEST-*.xml"/>
</fileset>
<fileset dir="${HYBRIS_LOG_DIR}/ci-it/raw">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${HYBRIS_LOG_DIR}/ci"/>
</junitreport>
<copy todir="${HYBRIS_LOG_DIR}/ci">
<fileset dir="${HYBRIS_LOG_DIR}/ci-ut">
<include name="jacoco-ut.exec"/>
</fileset>
<fileset dir="${HYBRIS_LOG_DIR}/ci-it">
<include name="jacoco-it.exec"/>
</fileset>
</copy>
<!--Generate JaCoCo coverage-->
<jacoco:merge destfile="${HYBRIS_LOG_DIR}/ci/merged.exec">
<fileset dir="${HYBRIS_LOG_DIR}/ci" includes="*.exec"/>
</jacoco:merge>
<generateCoverageReport name="ci" processFolder="${HYBRIS_LOG_DIR}/ci" processFile="merged.exec"/>
<echo message="==========================================================="/>
<echo message="SUMMARY JUNIT REPORT: ${HYBRIS_LOG_DIR}/ci/index.html"/>
<echo message="SUMMARY JACOCO REPORT: ${HYBRIS_LOG_DIR}/ci/jacoco-report/index.html"/>
<echo message="==========================================================="/>
</target>
<macrodef name="generateCoverageReport">
<attribute name="processFolder"/>
<attribute name="processFile"/>
<attribute name="name"/>
<sequential>
<jacoco:report>
<executiondata>
<file file="@{processFolder}/@{processFile}"/>
</executiondata>
<structure name="@{name}">
<classfiles>
<fileset dir="${HYBRIS_BIN_DIR}/custom">
<include name="**/*.class"/>
<exclude name="**/*Test.class"/>
<exclude name="**/test/**"/>
<exclude name="**/testsrc/**"/>
<exclude name="**/jalo/**"/>
<exclude name="**/constants/**"/>
<exclude name="**/de/hybris/**"/>
<exclude name="**/com/hybris/**"/>
<exclude name="**/org/apache/**"/>
<exclude name="**/gensrc/**"/>
<exclude name="**/Generated*.class"/>
<exclude name="**/dto/idoc/**"/>
</fileset>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${HYBRIS_BIN_DIR}/custom">
<include name="**/*.java"/>
<exclude name="**/de/hybris/**"/>
<exclude name="**/com/hybris/**"/>
<exclude name="**/org/apache/**"/>
</fileset>
</sourcefiles>
</structure>
<html destdir="@{processFolder}/jacoco-report"/>
</jacoco:report>
</sequential>
</macrodef>
<macrodef name="replace_test_extension_property_if_specified">
<sequential>
<condition property="testclasses.all.custom.extensions" value="false">
<isset property="testclasses.all.custom.extensions"/>
</condition>
<if>
<equals arg1="${testclasses.all.custom.extensions}" arg2="true"/>
<then>
<get_list_of_extensions_for_test returnProperty="testclasses.extensions"/>
</then>
</if>
</sequential>
</macrodef>
<macrodef name="get_list_of_extensions_for_test">
<attribute name="returnProperty"/>
<sequential>
<var name="@{returnProperty}" unset="true"/>
<sequential>
<!-- Get list of all extensions in custom folder with tests inside, excluding web tests -->
<pathconvert property="custom.regular.extensions.for.tests" pathsep=",">
<dirset dir="${HYBRIS_BIN_DIR}/custom">
<include name="**/testsrc"/>
<exclude name="**/web/**"/>
<exclude name="**/backoffice/**"/>
</dirset>
<mapper type="regexp"
from=".+/(.+)/testsrc$"
to="\1"/>
</pathconvert>
<!-- Get list of storefront/backoffice extensions in custom folder with web tests inside -->
<pathconvert property="custom.web.extensions.for.tests" pathsep=",">
<dirset dir="${HYBRIS_BIN_DIR}/custom">
<include name="**/**storefront**/**/testsrc"/>
<include name="**/backoffice/**/testsrc"/>
</dirset>
<mapper type="regexp"
from=".+/(.+)/.+/testsrc$"
to="\1"/>
</pathconvert>
<!-- Get list of addons extensions in custom folder with web tests inside -->
<pathconvert property="custom.addons.extensions.for.tests" pathsep=",">
<dirset dir="${HYBRIS_BIN_DIR}/custom">
<include name="**/acceleratoraddon/**/testsrc"/>
</dirset>
<mapper type="regexp"
from=".+/(.+)/.+/.+/testsrc$"
to="\1"/>
</pathconvert>
<!-- Concatenate all found extensions in property and remove double commas -->
<property name="custom.extensions.for.tests"
value="${custom.regular.extensions.for.tests},${custom.web.extensions.for.tests},${custom.addons.extensions.for.tests}"/>
<propertyregex property="custom.extensions.for.tests"
input="${custom.extensions.for.tests}"
regexp=",,"
replace=","
override="true"
global="true"/>
<!-- Create string of all enabled extension with prefix/suffix, so it can be used for contains check-->
<property name="tests.prefixed.extension.names" value=";${extension.names};"/>
<!-- Filter extensions which are not enabled -->
<for list="${custom.extensions.for.tests}" param="custom.test.extension" delimiter=",">
<sequential>
<if>
<not>
<contains string="${tests.prefixed.extension.names}"
substring=";@{custom.test.extension};"/>
</not>
<then>
<!-- Remove extension from middle of the list -->
<propertyregex property="custom.extensions.for.tests"
input="${custom.extensions.for.tests}"
regexp=",@{custom.test.extension},"
replace=","
override="true"
global="true"/>
<!-- Remove extension from end of the list -->
<propertyregex property="custom.extensions.for.tests"
input="${custom.extensions.for.tests}"
regexp=",@{custom.test.extension}$"
replace=""
override="true"
global="true"/>
<!-- Remove extension from start of the list -->
<propertyregex property="custom.extensions.for.tests"
input="${custom.extensions.for.tests}"
regexp="^@{custom.test.extension},"
replace=""
override="true"
global="true"/>
</then>
</if>
</sequential>
</for>
<!-- Remove last comma -->
<propertyregex property="custom.extensions.for.tests"
input="${custom.extensions.for.tests}"
regexp=",$"
replace=""
override="true"
global="true"/>
<property name="@{returnProperty}" value="${custom.extensions.for.tests}"/>
</sequential>
</sequential>
</macrodef>
|
For jacoco coverage reports you must add jacoco agent with all dependencies into your classpath. For example, you can add jacoco into your external-dependencies.xml
and use ant updateMavenDependencies
command to download dependencies.
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
|
<properties>
<asm.version>7.1</asm.version>
<jacoco.version>0.8.4</jacoco.version>
</properties>
<packaging>jar</packaging>
<dependencies>
<!-- JaCoCo-->
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.core</artifactId>
<version>${jacoco.version}</version>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.ant</artifactId>
<version>${jacoco.version}</version>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.report</artifactId>
<version>${jacoco.version}</version>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>${jacoco.version}</version>
<scope>test</scope>
</dependency>
<!-- ASM -->
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-tree</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-commons</artifactId>
<version>${asm.version}</version>
</dependency>
</dependencies>
|