From f4a979cb993db894b73c779cfc7676691ba5eb23 Mon Sep 17 00:00:00 2001 From: Gyorgy Mora Date: Wed, 11 May 2022 16:47:38 +0200 Subject: [PATCH 1/4] add tags to failed init descriptors too --- .../helmethair/scalatest/descriptor/ScalatestDescriptor.java | 3 ++- .../scalatest/descriptor/ScalatestFailedInitDescriptor.java | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/co/helmethair/scalatest/descriptor/ScalatestDescriptor.java b/src/main/java/co/helmethair/scalatest/descriptor/ScalatestDescriptor.java index 6e58d41..d30fc87 100644 --- a/src/main/java/co/helmethair/scalatest/descriptor/ScalatestDescriptor.java +++ b/src/main/java/co/helmethair/scalatest/descriptor/ScalatestDescriptor.java @@ -16,6 +16,7 @@ public abstract class ScalatestDescriptor implements TestDescriptor { private final UniqueId id; private TestDescriptor parentDescriptor = null; private Set childDescriptors = new HashSet(); + protected Set tags = new HashSet<>(); protected ScalatestDescriptor(UniqueId id) { this.id = id; @@ -75,7 +76,7 @@ public Set getChildren() { @Override public Set getTags() { - return new HashSet<>(); + return tags; } @Override diff --git a/src/main/java/co/helmethair/scalatest/descriptor/ScalatestFailedInitDescriptor.java b/src/main/java/co/helmethair/scalatest/descriptor/ScalatestFailedInitDescriptor.java index 87b847e..a48ac8f 100644 --- a/src/main/java/co/helmethair/scalatest/descriptor/ScalatestFailedInitDescriptor.java +++ b/src/main/java/co/helmethair/scalatest/descriptor/ScalatestFailedInitDescriptor.java @@ -1,18 +1,21 @@ package co.helmethair.scalatest.descriptor; import org.junit.platform.engine.TestSource; +import org.junit.platform.engine.TestTag; import org.junit.platform.engine.support.descriptor.MethodSource; import java.util.Optional; +import java.util.Set; public class ScalatestFailedInitDescriptor extends ScalatestDescriptor { private final Throwable cause; private final String suiteId; - public ScalatestFailedInitDescriptor(Throwable cause, String name) { + public ScalatestFailedInitDescriptor(Throwable cause, String name, Set tags) { super(ENGINE_ID.append("failed", name)); this.cause = cause; this.suiteId = name; + this.tags = tags; } @Override From 81e819f238ddf9226b50d2892e1d1d6eb22f7acd Mon Sep 17 00:00:00 2001 From: Gyorgy Mora Date: Wed, 11 May 2022 16:48:43 +0200 Subject: [PATCH 2/4] extract and add tags to failed init descriptors --- .../scalatest/runtime/Discovery.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/co/helmethair/scalatest/runtime/Discovery.java b/src/main/java/co/helmethair/scalatest/runtime/Discovery.java index ce5ef49..a54f73a 100644 --- a/src/main/java/co/helmethair/scalatest/runtime/Discovery.java +++ b/src/main/java/co/helmethair/scalatest/runtime/Discovery.java @@ -8,8 +8,10 @@ import org.junit.platform.engine.TestDescriptor; import org.junit.platform.engine.TestTag; import org.scalatest.Suite; +import org.scalatest.TagAnnotation; import scala.Option; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Set; @@ -23,15 +25,16 @@ public ScalatestEngineDescriptor discover(ScalatestEngineDescriptor engineDescri Suite suite = ((Suite) classLoader.loadClass(c.getName()).newInstance()); addSuite(suite, engineDescriptor); } catch (Throwable e) { - addFailedInit(e, c.getName(), engineDescriptor); + addFailedInit(e, c, engineDescriptor); } }); return engineDescriptor; } - private void addFailedInit(Throwable cause, String className, TestDescriptor parent) { - ScalatestFailedInitDescriptor failed = new ScalatestFailedInitDescriptor(cause, className); + private void addFailedInit(Throwable cause, Class clazz, TestDescriptor parent) { + String className = clazz.getName(); + ScalatestFailedInitDescriptor failed = new ScalatestFailedInitDescriptor(cause, className, extractTags(clazz)); linkChild(parent, failed); } @@ -43,7 +46,7 @@ private void addSuite(Suite suite, TestDescriptor parent) { try { addSuite(scalatestNestedSuite, scalatestSuiteDescriptor); } catch (Throwable e) { - addFailedInit(e, scalatestNestedSuite.getClass().getName(), scalatestSuiteDescriptor); + addFailedInit(e, scalatestNestedSuite.getClass(), scalatestSuiteDescriptor); } }); } @@ -58,6 +61,13 @@ private void addTests(ScalatestSuiteDescriptor suite, Set testNames) { linkChild(suite, new ScalatestTestDescriptor(suite, testName, getTags(suite.getScalasuite(), testName)))); } + private Set extractTags(Class clazz) { + return Arrays.stream(clazz.getAnnotations()).filter(a -> + a.annotationType().isAnnotationPresent(TagAnnotation.class) + ).map(a -> TestTag.create(a.annotationType().getName())) + .collect(Collectors.toSet()); + } + private Set getTags(Suite scalasuite, String testName) { Option> tagSetOption = scalasuite.tags().get(testName); if (tagSetOption.isDefined()) { From 2bd866e83004d0d64ca4bc6f843dec74c116b0e4 Mon Sep 17 00:00:00 2001 From: Gyorgy Mora Date: Wed, 11 May 2022 16:49:19 +0200 Subject: [PATCH 3/4] add verification for failed test for `TestExecutionListener` type listener --- .../java/co/helmethair/scalatest/helper/TestHelpers.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/test/java/co/helmethair/scalatest/helper/TestHelpers.java b/src/test/java/co/helmethair/scalatest/helper/TestHelpers.java index 9576b61..78f4edf 100644 --- a/src/test/java/co/helmethair/scalatest/helper/TestHelpers.java +++ b/src/test/java/co/helmethair/scalatest/helper/TestHelpers.java @@ -128,6 +128,10 @@ default void verifyTestStartReported(String testIdSuffix, TestExecutionListener verifyTestStartReported(testIdSuffix, listener, atLeastOnce()); } + default void verifyTestFailReported(String testIdSuffix, TestExecutionListener listener) { + verifyTestFailReported(testIdSuffix, listener, atLeastOnce()); + } + default void verifyTestStartNotReported(String testIdSuffix, TestExecutionListener listener) { verifyTestStartReported(testIdSuffix, listener, never()); } @@ -138,6 +142,11 @@ default void verifyTestStartReported(String testIdSuffix, TestExecutionListener ); } + default void verifyTestFailReported(String testIdSuffix, TestExecutionListener listener, VerificationMode mode) { + verify(listener, mode).executionFinished( + argThat(a -> a.getUniqueId().endsWith(testIdSuffix)), argThat(a -> a.getStatus() == TestExecutionResult.Status.FAILED)); + } + default void verifyTestFailReported(String testIdsuffix, TestEngineExecutionListener listener) { verifyTestFailReportedWith(testIdsuffix, listener, null); } From a26414d10bad718f430b52e0dcd54842d7f5b083 Mon Sep 17 00:00:00 2001 From: Gyorgy Mora Date: Wed, 11 May 2022 16:50:18 +0200 Subject: [PATCH 4/4] test that tagged classes are recorded as failed when they throw error during class initialization --- .../scalatest/ClassInitializationErrorTest.java | 15 +++++++++++++++ .../co/helmethair/scalatest/SuitLevelTag.java | 13 +++++++++++++ src/test/scala/tests/InitErrorTaggedTest.scala | 16 ++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 src/test/java/co/helmethair/scalatest/SuitLevelTag.java create mode 100644 src/test/scala/tests/InitErrorTaggedTest.scala diff --git a/src/test/java/co/helmethair/scalatest/ClassInitializationErrorTest.java b/src/test/java/co/helmethair/scalatest/ClassInitializationErrorTest.java index 391e93c..1a778bd 100644 --- a/src/test/java/co/helmethair/scalatest/ClassInitializationErrorTest.java +++ b/src/test/java/co/helmethair/scalatest/ClassInitializationErrorTest.java @@ -6,6 +6,9 @@ import org.junit.platform.engine.EngineDiscoveryRequest; import org.junit.platform.engine.ExecutionRequest; import org.junit.platform.engine.TestDescriptor; +import org.junit.platform.launcher.TestExecutionListener; + +import java.util.Arrays; import static org.mockito.Mockito.spy; @@ -24,4 +27,16 @@ void failedToInitializeClass() { verifyTestStartReported(testId, listener); verifyTestFailReported(testId, listener); } + + @Test + void failedToInitializeTaggedClass() { + TestExecutionListener listener = spy(new TestExecutionListener() { + }); + + launch(Arrays.asList("tests.InitErrorTaggedTest"), Arrays.asList("co.helmethair.scalatest.SuitLevelTag"), Arrays.asList(), listener); + + String testId = "[engine:scalatest]/[failed:tests.InitErrorTaggedTest]"; + verifyTestStartReported(testId, listener); + verifyTestFailReported(testId, listener); + } } diff --git a/src/test/java/co/helmethair/scalatest/SuitLevelTag.java b/src/test/java/co/helmethair/scalatest/SuitLevelTag.java new file mode 100644 index 0000000..2992f29 --- /dev/null +++ b/src/test/java/co/helmethair/scalatest/SuitLevelTag.java @@ -0,0 +1,13 @@ +package co.helmethair.scalatest; + +import org.scalatest.TagAnnotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@TagAnnotation +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD, ElementType.TYPE}) +public @interface SuitLevelTag { } diff --git a/src/test/scala/tests/InitErrorTaggedTest.scala b/src/test/scala/tests/InitErrorTaggedTest.scala new file mode 100644 index 0000000..5ee313e --- /dev/null +++ b/src/test/scala/tests/InitErrorTaggedTest.scala @@ -0,0 +1,16 @@ +package tests + +import co.helmethair.scalatest.SuitLevelTag +import co.helmethair.scalatest.helper.RegisterCall +import org.scalatest.funspec.AnyFunSpec + +@SuitLevelTag +class InitErrorTaggedTest extends AnyFunSpec with RegisterCall { + //throw some serious stuff during instantiation + throw new InternalError() + describe("InitErrorTaggedTest") { + it("never runs") { + register() + } + } +}