Skip to content

Commit 8d2b769

Browse files
daniel-mohedanodevflow.devflow-routing-intake
andauthored
Add display name as parameter in JUnit5 dynamic tests (#10649)
feat: add display name as parameter for dynamic tests chore: lint fix: tag name Merge branch 'master' into daniel.mohedano/junit-dynamic-tests Co-authored-by: devflow.devflow-routing-intake <devflow.devflow-routing-intake@kubernetes.us1.ddbuild.io>
1 parent de4d177 commit 8d2b769

File tree

7 files changed

+60
-13
lines changed

7 files changed

+60
-13
lines changed

dd-java-agent/instrumentation/junit/junit-5/junit-5-spock-2.0/src/main/java/datadog/trace/instrumentation/junit5/SpockTracingListener.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ private void testCaseExecutionStarted(final TestDescriptor testDescriptor) {
119119
private void testMethodExecutionStarted(TestDescriptor testDescriptor, MethodSource testSource) {
120120
TestDescriptor suiteDescriptor = SpockUtils.getSpecDescriptor(testDescriptor);
121121
String displayName = testDescriptor.getDisplayName();
122-
String testParameters = JUnitPlatformUtils.getParameters(testSource, displayName);
122+
String testParameters =
123+
JUnitPlatformUtils.getParameters(testDescriptor, testSource, displayName);
123124
List<String> tags = JUnitPlatformUtils.getTags(testDescriptor);
124125
TestSourceData testSourceData = SpockUtils.toTestSourceData(testDescriptor);
125126

@@ -221,7 +222,8 @@ private void testMethodExecutionSkipped(
221222
final TestDescriptor testDescriptor, final MethodSource methodSource, final String reason) {
222223
TestDescriptor suiteDescriptor = SpockUtils.getSpecDescriptor(testDescriptor);
223224
String displayName = testDescriptor.getDisplayName();
224-
String testParameters = JUnitPlatformUtils.getParameters(methodSource, displayName);
225+
String testParameters =
226+
JUnitPlatformUtils.getParameters(testDescriptor, methodSource, displayName);
225227
List<String> tags =
226228
testDescriptor.getTags().stream().map(TestTag::getName).collect(Collectors.toList());
227229
TestSourceData testSourceData = SpockUtils.toTestSourceData(testDescriptor);

dd-java-agent/instrumentation/junit/junit-5/junit-5-spock-2.0/src/main/java/datadog/trace/instrumentation/junit5/SpockUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ public static TestIdentifier toTestIdentifier(TestDescriptor testDescriptor) {
8989
MethodSource methodSource = (MethodSource) testSource;
9090
String testSuiteName = methodSource.getClassName();
9191
String displayName = spockNode.getDisplayName();
92-
String testParameters = JUnitPlatformUtils.getParameters(methodSource, displayName);
92+
String testParameters =
93+
JUnitPlatformUtils.getParameters(testDescriptor, methodSource, displayName);
9394
return new TestIdentifier(testSuiteName, displayName, testParameters);
9495

9596
} else {

dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/main/java/datadog/trace/instrumentation/junit5/JUnitPlatformUtils.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,14 @@ private static Method getTestMethod(MethodSource methodSource) {
148148
}
149149
}
150150

151-
public static String getParameters(MethodSource methodSource, String displayName) {
152-
if (methodSource.getMethodParameterTypes() == null
153-
|| methodSource.getMethodParameterTypes().isEmpty()) {
154-
return null;
151+
public static String getParameters(
152+
TestDescriptor testDescriptor, MethodSource methodSource, String displayName) {
153+
if (isDynamicTest(testDescriptor)
154+
|| (methodSource.getMethodParameterTypes() != null
155+
&& !methodSource.getMethodParameterTypes().isEmpty())) {
156+
return "{\"metadata\":{\"test_name\":" + toJson(displayName) + "}}";
155157
}
156-
return "{\"metadata\":{\"test_name\":" + toJson(displayName) + "}}";
158+
return null;
157159
}
158160

159161
@Nullable
@@ -164,7 +166,7 @@ public static TestIdentifier toTestIdentifier(TestDescriptor testDescriptor) {
164166
String testSuiteName = methodSource.getClassName();
165167
String testName = methodSource.getMethodName();
166168
String displayName = testDescriptor.getDisplayName();
167-
String testParameters = getParameters(methodSource, displayName);
169+
String testParameters = getParameters(testDescriptor, methodSource, displayName);
168170
return new TestIdentifier(testSuiteName, testName, testParameters);
169171

170172
} else {
@@ -240,6 +242,12 @@ public static boolean isParameterizedTest(TestDescriptor testDescriptor) {
240242
return "test-template".equals(lastSegment.getType());
241243
}
242244

245+
public static boolean isDynamicTest(TestDescriptor testDescriptor) {
246+
// retries add a "retry-attempt" segment at the end of the uniqueId, so the "dynamic-test"
247+
// segment might not be found in position (segments.size() - 1)
248+
return getIDSegmentValue(testDescriptor, "dynamic-test") != null;
249+
}
250+
243251
public static boolean isRetry(TestDescriptor testDescriptor) {
244252
return getIDSegmentValue(testDescriptor, RETRY_DESCRIPTOR_ID_SUFFIX) != null;
245253
}

dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/main/java/datadog/trace/instrumentation/junit5/TracingListener.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import datadog.trace.api.civisibility.config.TestSourceData;
44
import datadog.trace.api.civisibility.execution.TestExecutionHistory;
55
import datadog.trace.api.civisibility.telemetry.tag.TestFrameworkInstrumentation;
6+
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
7+
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
8+
import datadog.trace.bootstrap.instrumentation.api.Tags;
69
import java.util.List;
710
import java.util.stream.Collectors;
811
import org.junit.platform.engine.EngineExecutionListener;
@@ -119,7 +122,8 @@ private void testMethodExecutionStarted(TestDescriptor testDescriptor, MethodSou
119122

120123
String displayName = testDescriptor.getDisplayName();
121124
String testName = testSource.getMethodName();
122-
String testParameters = JUnitPlatformUtils.getParameters(testSource, displayName);
125+
String testParameters =
126+
JUnitPlatformUtils.getParameters(testDescriptor, testSource, displayName);
123127
List<String> tags = JUnitPlatformUtils.getTags(testDescriptor);
124128
TestSourceData testSourceData = JUnitPlatformUtils.toTestSourceData(testDescriptor);
125129

@@ -136,6 +140,13 @@ private void testMethodExecutionStarted(TestDescriptor testDescriptor, MethodSou
136140
testSourceData,
137141
null,
138142
TestEventsHandlerHolder.getExecutionHistory(testDescriptor));
143+
144+
if (JUnitPlatformUtils.isDynamicTest(testDescriptor)) {
145+
AgentSpan span = AgentTracer.activeSpan();
146+
if (span != null) {
147+
span.setTag(Tags.TEST_JUNIT_IS_DYNAMIC, true);
148+
}
149+
}
139150
}
140151

141152
private void testCaseExecutionFinished(
@@ -224,7 +235,8 @@ private void testMethodExecutionSkipped(
224235

225236
String displayName = testDescriptor.getDisplayName();
226237
String testName = testSource.getMethodName();
227-
String testParameters = JUnitPlatformUtils.getParameters(testSource, displayName);
238+
String testParameters =
239+
JUnitPlatformUtils.getParameters(testDescriptor, testSource, displayName);
228240
List<String> tags =
229241
testDescriptor.getTags().stream().map(TestTag::getName).collect(Collectors.toList());
230242
TestSourceData testSourceData = JUnitPlatformUtils.toTestSourceData(testDescriptor);
@@ -242,5 +254,12 @@ private void testMethodExecutionSkipped(
242254
testSourceData,
243255
reason,
244256
TestEventsHandlerHolder.getExecutionHistory(testDescriptor));
257+
258+
if (JUnitPlatformUtils.isDynamicTest(testDescriptor)) {
259+
AgentSpan span = AgentTracer.activeSpan();
260+
if (span != null) {
261+
span.setTag(Tags.TEST_JUNIT_IS_DYNAMIC, true);
262+
}
263+
}
245264
}
246265
}

dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/test/resources/test-factory/events.ftl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,10 @@
118118
"test.final_status" : "pass",
119119
"test.framework" : "junit5",
120120
"test.framework_version" : ${content_meta_test_framework_version},
121+
"test.junit5.is_dynamic" : "true",
121122
"test.module" : "junit-5.3",
122123
"test.name" : "test_factory",
124+
"test.parameters" : "{\"metadata\":{\"test_name\":\"dynamic_test_succeed\"}}",
123125
"test.source.file" : "dummy_source_path",
124126
"test.source.method" : "test_factory()Ljava/lang/Iterable;",
125127
"test.status" : "pass",
@@ -166,8 +168,10 @@
166168
"test.final_status" : "pass",
167169
"test.framework" : "junit5",
168170
"test.framework_version" : ${content_meta_test_framework_version},
171+
"test.junit5.is_dynamic" : "true",
169172
"test.module" : "junit-5.3",
170173
"test.name" : "test_factory",
174+
"test.parameters" : "{\"metadata\":{\"test_name\":\"dynamic_test_succeed\"}}",
171175
"test.source.file" : "dummy_source_path",
172176
"test.source.method" : "test_factory()Ljava/lang/Iterable;",
173177
"test.status" : "pass",
@@ -196,4 +200,4 @@
196200
},
197201
"type" : "test",
198202
"version" : 2
199-
} ]
203+
} ]

dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/test/resources/test-retry-factory/events.ftl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,10 @@
118118
"test.final_status" : "pass",
119119
"test.framework" : "junit5",
120120
"test.framework_version" : ${content_meta_test_framework_version},
121+
"test.junit5.is_dynamic" : "true",
121122
"test.module" : "junit-5.3",
122123
"test.name" : "test_factory",
124+
"test.parameters" : "{\"metadata\":{\"test_name\":\"dynamic_test_succeed\"}}",
123125
"test.source.file" : "dummy_source_path",
124126
"test.source.method" : "test_factory()Ljava/lang/Iterable;",
125127
"test.status" : "pass",
@@ -169,8 +171,10 @@
169171
"test.failure_suppressed" : "true",
170172
"test.framework" : "junit5",
171173
"test.framework_version" : ${content_meta_test_framework_version},
174+
"test.junit5.is_dynamic" : "true",
172175
"test.module" : "junit-5.3",
173176
"test.name" : "test_factory",
177+
"test.parameters" : "{\"metadata\":{\"test_name\":\"dynamic_test_failed\"}}",
174178
"test.source.file" : "dummy_source_path",
175179
"test.source.method" : "test_factory()Ljava/lang/Iterable;",
176180
"test.status" : "fail",
@@ -220,9 +224,11 @@
220224
"test.failure_suppressed" : "true",
221225
"test.framework" : "junit5",
222226
"test.framework_version" : ${content_meta_test_framework_version},
227+
"test.junit5.is_dynamic" : "true",
223228
"test.is_retry" : "true",
224229
"test.module" : "junit-5.3",
225230
"test.name" : "test_factory",
231+
"test.parameters" : "{\"metadata\":{\"test_name\":\"dynamic_test_failed\"}}",
226232
"test.retry_reason" : "auto_test_retry",
227233
"test.source.file" : "dummy_source_path",
228234
"test.source.method" : "test_factory()Ljava/lang/Iterable;",
@@ -273,9 +279,11 @@
273279
"test.failure_suppressed" : "true",
274280
"test.framework" : "junit5",
275281
"test.framework_version" : ${content_meta_test_framework_version},
282+
"test.junit5.is_dynamic" : "true",
276283
"test.is_retry" : "true",
277284
"test.module" : "junit-5.3",
278285
"test.name" : "test_factory",
286+
"test.parameters" : "{\"metadata\":{\"test_name\":\"dynamic_test_failed\"}}",
279287
"test.retry_reason" : "auto_test_retry",
280288
"test.source.file" : "dummy_source_path",
281289
"test.source.method" : "test_factory()Ljava/lang/Iterable;",
@@ -326,9 +334,11 @@
326334
"test.failure_suppressed" : "true",
327335
"test.framework" : "junit5",
328336
"test.framework_version" : ${content_meta_test_framework_version},
337+
"test.junit5.is_dynamic" : "true",
329338
"test.is_retry" : "true",
330339
"test.module" : "junit-5.3",
331340
"test.name" : "test_factory",
341+
"test.parameters" : "{\"metadata\":{\"test_name\":\"dynamic_test_failed\"}}",
332342
"test.retry_reason" : "auto_test_retry",
333343
"test.source.file" : "dummy_source_path",
334344
"test.source.method" : "test_factory()Ljava/lang/Iterable;",
@@ -380,9 +390,11 @@
380390
"test.framework" : "junit5",
381391
"test.framework_version" : ${content_meta_test_framework_version},
382392
"test.has_failed_all_retries" : "true",
393+
"test.junit5.is_dynamic" : "true",
383394
"test.is_retry" : "true",
384395
"test.module" : "junit-5.3",
385396
"test.name" : "test_factory",
397+
"test.parameters" : "{\"metadata\":{\"test_name\":\"dynamic_test_failed\"}}",
386398
"test.retry_reason" : "auto_test_retry",
387399
"test.source.file" : "dummy_source_path",
388400
"test.source.method" : "test_factory()Ljava/lang/Iterable;",
@@ -412,4 +424,4 @@
412424
},
413425
"type" : "test",
414426
"version" : 2
415-
} ]
427+
} ]

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/Tags.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public class Tags {
9898
public static final String TEST_IS_NEW = "test.is_new";
9999
public static final String TEST_IS_RETRY = "test.is_retry";
100100
public static final String TEST_RETRY_REASON = "test.retry_reason";
101+
public static final String TEST_JUNIT_IS_DYNAMIC = "test.junit5.is_dynamic";
101102
public static final String TEST_IS_MODIFIED = "test.is_modified";
102103
public static final String TEST_HAS_FAILED_ALL_RETRIES = "test.has_failed_all_retries";
103104
public static final String TEST_FAILURE_SUPPRESSED = "test.failure_suppressed";

0 commit comments

Comments
 (0)