Skip to content

Commit

Permalink
Mark JUnit 5 setup and teardown action spans as failed if there is an…
Browse files Browse the repository at this point in the history
… error (#8033)
  • Loading branch information
nikita-tkachenko-datadog authored Nov 28, 2024
1 parent 1ef67e5 commit 3ad344d
Show file tree
Hide file tree
Showing 17 changed files with 985 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,13 @@ abstract class CiVisibilityInstrumentationTest extends AgentTestRunner {
] + replacements

// uncomment to generate expected data templates
// def baseTemplatesPath = CiVisibilityInstrumentationTest.classLoader
// .getResource("test-succeed")
// .toURI()
// .schemeSpecificPart
// .replace('build/resources/test', 'src/test/resources')
// .replace('build/resources/latestDepTest', 'src/test/resources')
// .replace("test-succeed", testcaseName)
// CiVisibilityTestUtils.generateTemplates(baseTemplatesPath, events, coverages, additionalReplacements)
// return [:]
// def clazz = this.getClass()
// def resourceName = clazz.name.replace('.', '/') + ".class"
// def classfilePath = clazz.getResource(resourceName).toURI().schemeSpecificPart
// def modulePath = classfilePath.substring(0, classfilePath.indexOf("/build/classes"))
// def baseTemplatesPath = modulePath + "/src/test/resources/" + testcaseName
// CiVisibilityTestUtils.generateTemplates(baseTemplatesPath, events, coverages, additionalReplacements)
// return [:]

return CiVisibilityTestUtils.assertData(testcaseName, events, coverages, additionalReplacements)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ private static void traceInvocation(
agentSpan.setTag(Tags.TEST_CALLBACK, operationName);
try (AgentScope agentScope = AgentTracer.activateSpan(agentSpan)) {
invocation.proceed();
} catch (Throwable t) {
agentSpan.addThrowable(t);
throw t;
} finally {
agentSpan.finish();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class JUnit58Test extends CiVisibilityInstrumentationTest {
testcaseName | tests | expectedTracesCount
"test-before-each-after-each" | [TestSucceedBeforeEachAfterEach] | 2
"test-before-all-after-all" | [TestSucceedBeforeAllAfterAll] | 2
"test-failed-before-all" | [TestFailedBeforeAll] | 2
"test-failed-after-all" | [TestFailedAfterAll] | 2
"test-failed-before-each" | [TestFailedBeforeEach] | 2
"test-failed-after-each" | [TestFailedAfterEach] | 2
}

private static void runTests(List<Class<?>> tests) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.example;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;

public class TestFailedAfterAll {

@AfterAll
public static void tearDown() {
throw new RuntimeException("suite teardown failed");
}

@Test
public void test_succeed() {
assertTrue(true);
}

@Test
public void another_test_succeed() {
assertTrue(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.example;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

public class TestFailedAfterEach {

@AfterEach
public void tearDown() {
throw new RuntimeException("testcase teardown failed");
}

@Test
public void test_succeed() {
assertTrue(true);
}

@Test
public void another_test_succeed() {
assertTrue(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.example;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class TestFailedBeforeAll {

@BeforeAll
public static void setUp() {
throw new RuntimeException("suite setup failed");
}

@Test
public void test_succeed() {
assertTrue(true);
}

@Test
public void another_test_succeed() {
assertTrue(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.example;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class TestFailedBeforeEach {

@BeforeEach
public void setUp() {
throw new RuntimeException("testcase setup failed");
}

@Test
public void test_succeed() {
assertTrue(true);
}

@Test
public void another_test_succeed() {
assertTrue(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"_dd.trace_span_attribute_schema" : 0
},
"meta" : {
"_dd.p.tid" : ${content_meta__dd_p_tid},
"test.type" : "test",
"_dd.tracer_host" : ${content_meta__dd_tracer_host},
"test.status" : "pass",
Expand Down Expand Up @@ -46,9 +47,10 @@
"duration" : ${content_duration_2},
"error" : 0,
"metrics" : {
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count}
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count_2}
},
"meta" : {
"_dd.p.tid" : ${content_meta__dd_p_tid_2},
"test.type" : "test",
"test.module" : "junit-5.8",
"test.status" : "pass",
Expand Down Expand Up @@ -76,16 +78,20 @@
"duration" : ${content_duration_3},
"error" : 0,
"metrics" : {
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count}
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count_3},
"test.source.end" : 19,
"test.source.start" : 11
},
"meta" : {
"_dd.p.tid" : ${content_meta__dd_p_tid_3},
"test.type" : "test",
"test.source.file" : "dummy_source_path",
"test.module" : "junit-5.8",
"test.status" : "pass",
"test_session.name" : "session-name",
"env" : "none",
"dummy_ci_tag" : "dummy_ci_tag_value",
"test.codeowners" : "[\"owner1\",\"owner2\"]",
"library_version" : ${content_meta_library_version},
"component" : "junit",
"span.kind" : "test_suite_end",
Expand Down Expand Up @@ -114,7 +120,7 @@
"process_id" : ${content_metrics_process_id},
"_dd.profiling.enabled" : 0,
"_dd.trace_span_attribute_schema" : 0,
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count},
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count_4},
"test.source.end" : 18,
"test.source.start" : 12
},
Expand Down Expand Up @@ -161,7 +167,7 @@
"process_id" : ${content_metrics_process_id},
"_dd.profiling.enabled" : 0,
"_dd.trace_span_attribute_schema" : 0,
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count},
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count_5},
"test.source.end" : 18,
"test.source.start" : 12
},
Expand Down Expand Up @@ -203,6 +209,7 @@
"error" : 0,
"metrics" : { },
"meta" : {
"_dd.p.tid" : ${content_meta__dd_p_tid_4},
"test.callback" : "BeforeAll",
"library_version" : ${content_meta_library_version},
"env" : "none"
Expand All @@ -223,6 +230,7 @@
"error" : 0,
"metrics" : { },
"meta" : {
"_dd.p.tid" : ${content_meta__dd_p_tid_5},
"test.callback" : "AfterAll",
"library_version" : ${content_meta_library_version},
"env" : "none"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"_dd.trace_span_attribute_schema" : 0
},
"meta" : {
"_dd.p.tid" : ${content_meta__dd_p_tid},
"test.type" : "test",
"_dd.tracer_host" : ${content_meta__dd_tracer_host},
"test.status" : "pass",
Expand Down Expand Up @@ -49,6 +50,7 @@
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count_2}
},
"meta" : {
"_dd.p.tid" : ${content_meta__dd_p_tid_2},
"test.type" : "test",
"test.module" : "junit-5.8",
"test.status" : "pass",
Expand Down Expand Up @@ -76,16 +78,20 @@
"duration" : ${content_duration_3},
"error" : 0,
"metrics" : {
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count_3}
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count_3},
"test.source.end" : 19,
"test.source.start" : 11
},
"meta" : {
"_dd.p.tid" : ${content_meta__dd_p_tid_3},
"test.type" : "test",
"test.source.file" : "dummy_source_path",
"test.module" : "junit-5.8",
"test.status" : "pass",
"test_session.name" : "session-name",
"env" : "none",
"dummy_ci_tag" : "dummy_ci_tag_value",
"test.codeowners" : "[\"owner1\",\"owner2\"]",
"library_version" : ${content_meta_library_version},
"component" : "junit",
"span.kind" : "test_suite_end",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ ]
Loading

0 comments on commit 3ad344d

Please sign in to comment.