From e8c7265c64857ff334ebd5a99a1dfa4f74344944 Mon Sep 17 00:00:00 2001 From: Dmitry Baev Date: Wed, 31 Jan 2024 16:58:58 +0000 Subject: [PATCH] rft test commons (via #1001) --- .../cucumber4jvm/AllureCucumber4JvmTest.java | 63 ++++++-------- .../cucumber5jvm/AllureCucumber5JvmTest.java | 62 ++++++-------- .../cucumber6jvm/AllureCucumber6JvmTest.java | 62 ++++++-------- .../cucumber7jvm/AllureCucumber7JvmTest.java | 62 ++++++-------- .../java/io/qameta/allure/test/RunUtils.java | 85 ++++++++++++++++--- .../main/java/io/qameta/allure/Allure.java | 3 +- .../io/qameta/allure/util/ExceptionUtils.java | 2 +- .../io/qameta/allure/AllureLifecycleTest.java | 39 +++------ .../allure/StepLifecycleListenerTest.java | 54 ++---------- .../allure/jbehave/AllureJbehaveTest.java | 68 +++++++-------- .../allure/jbehave5/AllureJbehave5Test.java | 68 +++++++-------- .../AllureJunitPlatformTest.java | 45 ++++------ .../AllureJunitPlatformTestUtils.java | 70 ++++----------- .../allure/junit4/AllureJunit4Test.java | 35 +++----- .../allure/junit5/AllureJunit5Test.java | 51 ++++------- .../io/qameta/allure/karate/TestRunner.java | 54 +++++------- .../qameta/allure/spock/AllureSpockTest.java | 36 ++------ .../io/qameta/allure/spock2/AllureSpock2.java | 2 +- .../allure/spock2/AllureSpock2Test.java | 71 ++++++---------- .../allure/testng/AllureTestNgTest.java | 76 ++++++----------- 20 files changed, 399 insertions(+), 609 deletions(-) diff --git a/allure-cucumber4-jvm/src/test/java/io/qameta/allure/cucumber4jvm/AllureCucumber4JvmTest.java b/allure-cucumber4-jvm/src/test/java/io/qameta/allure/cucumber4jvm/AllureCucumber4JvmTest.java index da872dce..18940bc8 100644 --- a/allure-cucumber4-jvm/src/test/java/io/qameta/allure/cucumber4jvm/AllureCucumber4JvmTest.java +++ b/allure-cucumber4-jvm/src/test/java/io/qameta/allure/cucumber4jvm/AllureCucumber4JvmTest.java @@ -15,19 +15,16 @@ */ package io.qameta.allure.cucumber4jvm; -import cucumber.runtime.ClassFinder; import cucumber.runtime.FeaturePathFeatureSupplier; import cucumber.runtime.Runtime; import cucumber.runtime.io.MultiLoader; import cucumber.runtime.io.ResourceLoader; -import cucumber.runtime.io.ResourceLoaderClassFinder; import cucumber.runtime.model.FeatureLoader; import io.cucumber.core.model.FeatureWithLines; import io.cucumber.core.options.CommandlineOptionsParser; import io.cucumber.core.options.RuntimeOptions; import io.github.glytching.junit.extension.system.SystemProperty; import io.github.glytching.junit.extension.system.SystemPropertyExtension; -import io.qameta.allure.AllureLifecycle; import io.qameta.allure.Step; import io.qameta.allure.model.Attachment; import io.qameta.allure.model.FixtureResult; @@ -41,8 +38,7 @@ import io.qameta.allure.model.TestResult; import io.qameta.allure.test.AllureFeatures; import io.qameta.allure.test.AllureResults; -import io.qameta.allure.test.AllureResultsWriterStub; -import io.qameta.allure.test.AllureTestCommonsUtils; +import io.qameta.allure.test.RunUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.parallel.ResourceLock; @@ -719,38 +715,31 @@ void shouldSupportProvidedLabels() { @Step private AllureResults runFeature(final String featureResource, final String... moreOptions) { - - final AllureResultsWriterStub writer = new AllureResultsWriterStub(); - - final AllureLifecycle lifecycle = new AllureLifecycle(writer); - final AllureCucumber4Jvm cucumber4Jvm = new AllureCucumber4Jvm(lifecycle); - final ClassLoader classLoader = currentThread().getContextClassLoader(); - final ResourceLoader resourceLoader = new MultiLoader(classLoader); - final ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader); - final List opts = new ArrayList<>(Arrays.asList( - "--glue", "io.qameta.allure.cucumber4jvm.samples", - "--plugin", "null_summary" - )); - opts.addAll(Arrays.asList(moreOptions)); - final FeatureWithLines featureWithLines = FeatureWithLines.parse("src/test/resources/" + featureResource); - final RuntimeOptions options = new CommandlineOptionsParser() - .parse(opts.toArray(new String[]{})).addFeature(featureWithLines).build(); - - final FeaturePathFeatureSupplier supplier - = new FeaturePathFeatureSupplier(new FeatureLoader(resourceLoader), options); - - final Runtime runtime = Runtime.builder() - .withClassLoader(classLoader) - .withRuntimeOptions(options) - .withAdditionalPlugins(cucumber4Jvm) - .withFeatureSupplier(supplier) - .build(); - - runtime.run(); - - AllureTestCommonsUtils.attach(writer); - - return writer; + return RunUtils.runTests(lifecycle -> { + final AllureCucumber4Jvm cucumber4Jvm = new AllureCucumber4Jvm(lifecycle); + final ClassLoader classLoader = currentThread().getContextClassLoader(); + final ResourceLoader resourceLoader = new MultiLoader(classLoader); + final List opts = new ArrayList<>(Arrays.asList( + "--glue", "io.qameta.allure.cucumber4jvm.samples", + "--plugin", "null_summary" + )); + opts.addAll(Arrays.asList(moreOptions)); + final FeatureWithLines featureWithLines = FeatureWithLines.parse("src/test/resources/" + featureResource); + final RuntimeOptions options = new CommandlineOptionsParser() + .parse(opts.toArray(new String[]{})).addFeature(featureWithLines).build(); + + final FeaturePathFeatureSupplier supplier + = new FeaturePathFeatureSupplier(new FeatureLoader(resourceLoader), options); + + final Runtime runtime = Runtime.builder() + .withClassLoader(classLoader) + .withRuntimeOptions(options) + .withAdditionalPlugins(cucumber4Jvm) + .withFeatureSupplier(supplier) + .build(); + + runtime.run(); + }); } diff --git a/allure-cucumber5-jvm/src/test/java/io/qameta/allure/cucumber5jvm/AllureCucumber5JvmTest.java b/allure-cucumber5-jvm/src/test/java/io/qameta/allure/cucumber5jvm/AllureCucumber5JvmTest.java index f4fe1a15..988882c2 100644 --- a/allure-cucumber5-jvm/src/test/java/io/qameta/allure/cucumber5jvm/AllureCucumber5JvmTest.java +++ b/allure-cucumber5-jvm/src/test/java/io/qameta/allure/cucumber5jvm/AllureCucumber5JvmTest.java @@ -26,7 +26,6 @@ import io.cucumber.core.runtime.TimeServiceEventBus; import io.github.glytching.junit.extension.system.SystemProperty; import io.github.glytching.junit.extension.system.SystemPropertyExtension; -import io.qameta.allure.AllureLifecycle; import io.qameta.allure.Step; import io.qameta.allure.model.Attachment; import io.qameta.allure.model.FixtureResult; @@ -40,8 +39,7 @@ import io.qameta.allure.model.TestResult; import io.qameta.allure.test.AllureFeatures; import io.qameta.allure.test.AllureResults; -import io.qameta.allure.test.AllureResultsWriterStub; -import io.qameta.allure.test.AllureTestCommonsUtils; +import io.qameta.allure.test.RunUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.parallel.ResourceLock; @@ -720,38 +718,32 @@ void shouldSupportProvidedLabels() { @Step private AllureResults runFeature(final String featureResource, final String... moreOptions) { - - final AllureResultsWriterStub writer = new AllureResultsWriterStub(); - - final AllureLifecycle lifecycle = new AllureLifecycle(writer); - final AllureCucumber5Jvm cucumber5jvm = new AllureCucumber5Jvm(lifecycle); - final Supplier classLoader = ClassLoaders::getDefaultClassLoader; - final List opts = new ArrayList<>(Arrays.asList( - "--glue", "io.qameta.allure.cucumber5jvm.samples", - "--plugin", "null_summary" - )); - opts.addAll(Arrays.asList(moreOptions)); - final FeatureWithLines featureWithLines = FeatureWithLines.parse("src/test/resources/" + featureResource); - final RuntimeOptions options = new CommandlineOptionsParser() - .parse(opts.toArray(new String[]{})).addFeature(featureWithLines).build(); - - final EventBus bus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID); - final FeatureParser parser = new FeatureParser(bus::generateId); - final FeaturePathFeatureSupplier supplier - = new FeaturePathFeatureSupplier(classLoader, options, parser); - - final Runtime runtime = Runtime.builder() - .withClassLoader(classLoader) - .withRuntimeOptions(options) - .withAdditionalPlugins(cucumber5jvm) - .withFeatureSupplier(supplier) - .build(); - - runtime.run(); - - AllureTestCommonsUtils.attach(writer); - - return writer; + return RunUtils.runTests(lifecycle -> { + final AllureCucumber5Jvm cucumber5jvm = new AllureCucumber5Jvm(lifecycle); + final Supplier classLoader = ClassLoaders::getDefaultClassLoader; + final List opts = new ArrayList<>(Arrays.asList( + "--glue", "io.qameta.allure.cucumber5jvm.samples", + "--plugin", "null_summary" + )); + opts.addAll(Arrays.asList(moreOptions)); + final FeatureWithLines featureWithLines = FeatureWithLines.parse("src/test/resources/" + featureResource); + final RuntimeOptions options = new CommandlineOptionsParser() + .parse(opts.toArray(new String[]{})).addFeature(featureWithLines).build(); + + final EventBus bus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID); + final FeatureParser parser = new FeatureParser(bus::generateId); + final FeaturePathFeatureSupplier supplier + = new FeaturePathFeatureSupplier(classLoader, options, parser); + + final Runtime runtime = Runtime.builder() + .withClassLoader(classLoader) + .withRuntimeOptions(options) + .withAdditionalPlugins(cucumber5jvm) + .withFeatureSupplier(supplier) + .build(); + + runtime.run(); + }); } diff --git a/allure-cucumber6-jvm/src/test/java/io/qameta/allure/cucumber6jvm/AllureCucumber6JvmTest.java b/allure-cucumber6-jvm/src/test/java/io/qameta/allure/cucumber6jvm/AllureCucumber6JvmTest.java index b5a1ff4d..e742aeb0 100644 --- a/allure-cucumber6-jvm/src/test/java/io/qameta/allure/cucumber6jvm/AllureCucumber6JvmTest.java +++ b/allure-cucumber6-jvm/src/test/java/io/qameta/allure/cucumber6jvm/AllureCucumber6JvmTest.java @@ -26,7 +26,6 @@ import io.cucumber.core.runtime.TimeServiceEventBus; import io.github.glytching.junit.extension.system.SystemProperty; import io.github.glytching.junit.extension.system.SystemPropertyExtension; -import io.qameta.allure.AllureLifecycle; import io.qameta.allure.Step; import io.qameta.allure.model.Attachment; import io.qameta.allure.model.FixtureResult; @@ -40,8 +39,7 @@ import io.qameta.allure.model.TestResult; import io.qameta.allure.test.AllureFeatures; import io.qameta.allure.test.AllureResults; -import io.qameta.allure.test.AllureResultsWriterStub; -import io.qameta.allure.test.AllureTestCommonsUtils; +import io.qameta.allure.test.RunUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.parallel.ResourceLock; @@ -720,37 +718,31 @@ void shouldSupportProvidedLabels() { @Step private AllureResults runFeature(final String featureResource, final String... moreOptions) { - - final AllureResultsWriterStub writer = new AllureResultsWriterStub(); - - final AllureLifecycle lifecycle = new AllureLifecycle(writer); - final AllureCucumber6Jvm cucumber6jvm = new AllureCucumber6Jvm(lifecycle); - final Supplier classLoader = ClassLoaders::getDefaultClassLoader; - final List opts = new ArrayList<>(Arrays.asList( - "--glue", "io.qameta.allure.cucumber6jvm.samples", - "--plugin", "null_summary" - )); - opts.addAll(Arrays.asList(moreOptions)); - final FeatureWithLines featureWithLines = FeatureWithLines.parse("src/test/resources/" + featureResource); - final RuntimeOptions options = new CommandlineOptionsParser(System.out) - .parse(opts.toArray(new String[]{})).addFeature(featureWithLines).build(); - - final EventBus bus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID); - final FeatureParser parser = new FeatureParser(bus::generateId); - final FeaturePathFeatureSupplier supplier - = new FeaturePathFeatureSupplier(classLoader, options, parser); - - final Runtime runtime = Runtime.builder() - .withClassLoader(classLoader) - .withRuntimeOptions(options) - .withAdditionalPlugins(cucumber6jvm) - .withFeatureSupplier(supplier) - .build(); - - runtime.run(); - - AllureTestCommonsUtils.attach(writer); - - return writer; + return RunUtils.runTests(lifecycle -> { + final AllureCucumber6Jvm cucumber6jvm = new AllureCucumber6Jvm(lifecycle); + final Supplier classLoader = ClassLoaders::getDefaultClassLoader; + final List opts = new ArrayList<>(Arrays.asList( + "--glue", "io.qameta.allure.cucumber6jvm.samples", + "--plugin", "null_summary" + )); + opts.addAll(Arrays.asList(moreOptions)); + final FeatureWithLines featureWithLines = FeatureWithLines.parse("src/test/resources/" + featureResource); + final RuntimeOptions options = new CommandlineOptionsParser(System.out) + .parse(opts.toArray(new String[]{})).addFeature(featureWithLines).build(); + + final EventBus bus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID); + final FeatureParser parser = new FeatureParser(bus::generateId); + final FeaturePathFeatureSupplier supplier + = new FeaturePathFeatureSupplier(classLoader, options, parser); + + final Runtime runtime = Runtime.builder() + .withClassLoader(classLoader) + .withRuntimeOptions(options) + .withAdditionalPlugins(cucumber6jvm) + .withFeatureSupplier(supplier) + .build(); + + runtime.run(); + }); } } diff --git a/allure-cucumber7-jvm/src/test/java/io/qameta/allure/cucumber7jvm/AllureCucumber7JvmTest.java b/allure-cucumber7-jvm/src/test/java/io/qameta/allure/cucumber7jvm/AllureCucumber7JvmTest.java index 055fd7db..ff644c18 100644 --- a/allure-cucumber7-jvm/src/test/java/io/qameta/allure/cucumber7jvm/AllureCucumber7JvmTest.java +++ b/allure-cucumber7-jvm/src/test/java/io/qameta/allure/cucumber7jvm/AllureCucumber7JvmTest.java @@ -26,7 +26,6 @@ import io.cucumber.core.runtime.TimeServiceEventBus; import io.github.glytching.junit.extension.system.SystemProperty; import io.github.glytching.junit.extension.system.SystemPropertyExtension; -import io.qameta.allure.AllureLifecycle; import io.qameta.allure.Step; import io.qameta.allure.model.Attachment; import io.qameta.allure.model.FixtureResult; @@ -40,8 +39,7 @@ import io.qameta.allure.model.TestResult; import io.qameta.allure.test.AllureFeatures; import io.qameta.allure.test.AllureResults; -import io.qameta.allure.test.AllureResultsWriterStub; -import io.qameta.allure.test.AllureTestCommonsUtils; +import io.qameta.allure.test.RunUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.parallel.ResourceLock; @@ -721,38 +719,32 @@ void shouldSupportProvidedLabels() { private AllureResults runFeature(final String featureResource, final String... moreOptions) { - final AllureResultsWriterStub writer = new AllureResultsWriterStub(); - - final AllureLifecycle lifecycle = new AllureLifecycle(writer); - final AllureCucumber7Jvm cucumber7jvm = new AllureCucumber7Jvm(lifecycle); - final Supplier classLoader = ClassLoaders::getDefaultClassLoader; - final List opts = new ArrayList<>(Arrays.asList( - "--glue", "io.qameta.allure.cucumber7jvm.samples", - "--no-summary" - )); - opts.addAll(Arrays.asList(moreOptions)); - final FeatureWithLines featureWithLines = FeatureWithLines.parse("src/test/resources/" + featureResource); - final RuntimeOptions options = new CommandlineOptionsParser(System.out) - .parse(opts.toArray(new String[]{})).addFeature(featureWithLines).build(); - - final EventBus bus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID); - final FeatureParser parser = new FeatureParser(bus::generateId); - final FeaturePathFeatureSupplier supplier - = new FeaturePathFeatureSupplier(classLoader, options, parser); - - final Runtime runtime = Runtime.builder() - .withClassLoader(classLoader) - .withRuntimeOptions(options) - .withAdditionalPlugins(cucumber7jvm) - .withFeatureSupplier(supplier) - .build(); - - runtime.run(); - - AllureTestCommonsUtils.attach(writer); - - return writer; + return RunUtils.runTests(lifecycle -> { + final AllureCucumber7Jvm cucumber7jvm = new AllureCucumber7Jvm(lifecycle); + final Supplier classLoader = ClassLoaders::getDefaultClassLoader; + final List opts = new ArrayList<>(Arrays.asList( + "--glue", "io.qameta.allure.cucumber7jvm.samples", + "--no-summary" + )); + opts.addAll(Arrays.asList(moreOptions)); + final FeatureWithLines featureWithLines = FeatureWithLines.parse("src/test/resources/" + featureResource); + final RuntimeOptions options = new CommandlineOptionsParser(System.out) + .parse(opts.toArray(new String[]{})).addFeature(featureWithLines).build(); + + final EventBus bus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID); + final FeatureParser parser = new FeatureParser(bus::generateId); + final FeaturePathFeatureSupplier supplier + = new FeaturePathFeatureSupplier(classLoader, options, parser); + + final Runtime runtime = Runtime.builder() + .withClassLoader(classLoader) + .withRuntimeOptions(options) + .withAdditionalPlugins(cucumber7jvm) + .withFeatureSupplier(supplier) + .build(); + + runtime.run(); + }); } - } diff --git a/allure-java-commons-test/src/main/java/io/qameta/allure/test/RunUtils.java b/allure-java-commons-test/src/main/java/io/qameta/allure/test/RunUtils.java index 185a98b5..3905c64a 100644 --- a/allure-java-commons-test/src/main/java/io/qameta/allure/test/RunUtils.java +++ b/allure-java-commons-test/src/main/java/io/qameta/allure/test/RunUtils.java @@ -17,12 +17,15 @@ import io.qameta.allure.Allure; import io.qameta.allure.AllureLifecycle; +import io.qameta.allure.AllureResultsWriter; import io.qameta.allure.aspects.AttachmentsAspects; import io.qameta.allure.aspects.StepsAspects; import io.qameta.allure.model.TestResult; +import io.qameta.allure.util.ExceptionUtils; import java.util.UUID; import java.util.function.Consumer; +import java.util.function.Function; import java.util.stream.Stream; import static io.qameta.allure.util.ResultsUtils.getStatus; @@ -37,8 +40,21 @@ private RunUtils() { throw new IllegalStateException("do not instance"); } - public static AllureResults runWithinTestContext(final Runnable runnable) { - return runWithinTestContext( + public static AllureResults runTests( + final Allure.ThrowableContextRunnableVoid runnable) { + return runTests( + runnable, + Allure::setLifecycle, + StepsAspects::setLifecycle, + AttachmentsAspects::setLifecycle + ); + } + + public static AllureResults runTests( + final Function lifecycleFactory, + final Allure.ThrowableContextRunnableVoid runnable) { + return runTests( + lifecycleFactory, runnable, Allure::setLifecycle, StepsAspects::setLifecycle, @@ -47,18 +63,67 @@ public static AllureResults runWithinTestContext(final Runnable runnable) { } @SafeVarargs - public static AllureResults runWithinTestContext(final Runnable runnable, - final Consumer... configurers) { + public static AllureResults runTests( + final Allure.ThrowableContextRunnableVoid runnable, + final Consumer... configurers) { + return runTests(AllureLifecycle::new, runnable, configurers); + } + + @SafeVarargs + public static AllureResults runTests( + final Function lifecycleFactory, + final Allure.ThrowableContextRunnableVoid runnable, + final Consumer... configurers) { final AllureResultsWriterStub writer = new AllureResultsWriterStub(); - final AllureLifecycle lifecycle = new AllureLifecycle(writer); + final AllureLifecycle lifecycle = lifecycleFactory.apply(writer); + + final AllureLifecycle defaultLifecycle = Allure.getLifecycle(); + try { + Stream.of(configurers).forEach(configurer -> configurer.accept(lifecycle)); + + runnable.run(lifecycle); + + return writer; + } catch (Throwable e) { + throw ExceptionUtils.sneakyThrow(e); + } finally { + Stream.of(configurers).forEach(configurer -> configurer.accept(defaultLifecycle)); + AllureTestCommonsUtils.attach(writer); + } + } + + public static AllureResults runWithinTestContext( + final Runnable runnable) { + return runTests(lifecycle -> withTestContext(runnable, lifecycle)); + } + + public static AllureResults runWithinTestContext( + final Function lifecycleFactory, + final Runnable runnable) { + return runTests(lifecycleFactory, lifecycle -> withTestContext(runnable, lifecycle)); + } + + @SafeVarargs + public static AllureResults runWithinTestContext( + final Runnable runnable, + final Consumer... configurers) { + return runTests(lifecycle -> withTestContext(runnable, lifecycle), configurers); + } + + @SafeVarargs + public static AllureResults runWithinTestContext( + final Function lifecycleFactory, + final Runnable runnable, + final Consumer... configurers) { + return runTests(lifecycleFactory, lifecycle -> withTestContext(runnable, lifecycle), configurers); + } + + private static void withTestContext(final Runnable runnable, final AllureLifecycle lifecycle) { final String uuid = UUID.randomUUID().toString(); final TestResult result = new TestResult().setUuid(uuid); - final AllureLifecycle cached = Allure.getLifecycle(); try { - Stream.of(configurers).forEach(configurer -> configurer.accept(lifecycle)); - lifecycle.scheduleTestCase(result); lifecycle.startTestCase(uuid); @@ -72,11 +137,7 @@ public static AllureResults runWithinTestContext(final Runnable runnable, } finally { lifecycle.stopTestCase(uuid); lifecycle.writeTestCase(uuid); - - Stream.of(configurers).forEach(configurer -> configurer.accept(cached)); } - - return writer; } } diff --git a/allure-java-commons/src/main/java/io/qameta/allure/Allure.java b/allure-java-commons/src/main/java/io/qameta/allure/Allure.java index afc8ddbc..445d8b2c 100644 --- a/allure-java-commons/src/main/java/io/qameta/allure/Allure.java +++ b/allure-java-commons/src/main/java/io/qameta/allure/Allure.java @@ -185,8 +185,7 @@ public static T step(final ThrowableContextRunnable runnable getLifecycle().updateStep(s -> s .setStatus(getStatus(throwable).orElse(Status.BROKEN)) .setStatusDetails(getStatusDetails(throwable).orElse(null))); - ExceptionUtils.sneakyThrow(throwable); - return null; + throw ExceptionUtils.sneakyThrow(throwable); } finally { getLifecycle().stopStep(uuid); } diff --git a/allure-java-commons/src/main/java/io/qameta/allure/util/ExceptionUtils.java b/allure-java-commons/src/main/java/io/qameta/allure/util/ExceptionUtils.java index e658e8e2..f644cf7e 100644 --- a/allure-java-commons/src/main/java/io/qameta/allure/util/ExceptionUtils.java +++ b/allure-java-commons/src/main/java/io/qameta/allure/util/ExceptionUtils.java @@ -25,7 +25,7 @@ private ExceptionUtils() { } @SuppressWarnings("unchecked") - public static void sneakyThrow(final Throwable throwable) throws T { + public static T sneakyThrow(final Throwable throwable) throws T { throw (T) throwable; } } diff --git a/allure-java-commons/src/test/java/io/qameta/allure/AllureLifecycleTest.java b/allure-java-commons/src/test/java/io/qameta/allure/AllureLifecycleTest.java index cf8fa80b..02a74faa 100644 --- a/allure-java-commons/src/test/java/io/qameta/allure/AllureLifecycleTest.java +++ b/allure-java-commons/src/test/java/io/qameta/allure/AllureLifecycleTest.java @@ -20,7 +20,8 @@ import io.qameta.allure.model.StepResult; import io.qameta.allure.model.TestResult; import io.qameta.allure.model.TestResultContainer; -import io.qameta.allure.test.AllureResultsWriterStub; +import io.qameta.allure.test.AllureResults; +import io.qameta.allure.test.RunUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; @@ -34,7 +35,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.UUID; import java.util.concurrent.Callable; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CopyOnWriteArrayList; @@ -46,7 +46,6 @@ import java.util.stream.Collectors; import static io.qameta.allure.Allure.addStreamAttachmentAsync; -import static io.qameta.allure.Allure.setLifecycle; import static io.qameta.allure.test.TestData.randomId; import static io.qameta.allure.test.TestData.randomName; import static io.qameta.allure.test.TestData.randomString; @@ -296,36 +295,24 @@ void shouldCreateTestFixture() { .containsExactly(firstStepName, secondStepName); } - @SuppressWarnings("OptionalGetWithoutIsPresent") @Test void shouldAttachAsync() { final List> features = new CopyOnWriteArrayList<>(); - final AllureResultsWriterStub writer = new AllureResultsWriterStub(); - - final AllureLifecycle lifecycle = new AllureLifecycle(writer); - setLifecycle(lifecycle); - - final String uuid = UUID.randomUUID().toString(); - final TestResult result = new TestResult().setUuid(uuid); - - lifecycle.scheduleTestCase(result); - lifecycle.startTestCase(uuid); - - final String attachment1Content = randomString(100); - final String attachment2Content = randomString(100); final String attachment1Name = randomName(); final String attachment2Name = randomName(); - features.add(addStreamAttachmentAsync( - attachment1Name, "video/mp4", getStreamWithTimeout(2, attachment1Content))); - features.add(addStreamAttachmentAsync( - attachment2Name, "text/plain", getStreamWithTimeout(1, attachment2Content))); + final String attachment1Content = randomString(100); + final String attachment2Content = randomString(100); - lifecycle.stopTestCase(uuid); - lifecycle.writeTestCase(uuid); + final AllureResults writer = RunUtils.runWithinTestContext(() -> { + features.add(addStreamAttachmentAsync( + attachment1Name, "video/mp4", getStreamWithTimeout(2, attachment1Content))); + features.add(addStreamAttachmentAsync( + attachment2Name, "text/plain", getStreamWithTimeout(1, attachment2Content))); - allOf(features.toArray(new CompletableFuture[0])).join(); + allOf(features.toArray(new CompletableFuture[0])).join(); + }); final List attachments = writer.getTestResults().stream() .map(TestResult::getAttachments) @@ -351,7 +338,7 @@ void shouldAttachAsync() { final io.qameta.allure.model.Attachment attachment1 = attachments.stream() .filter(attachment -> Objects.equals(attachment.getName(), attachment1Name)) .findAny() - .get(); + .orElseThrow(); final byte[] actual1 = attachmentFiles.get(attachment1.getSource()); @@ -361,7 +348,7 @@ void shouldAttachAsync() { final Attachment attachment2 = attachments.stream() .filter(attachment -> Objects.equals(attachment.getName(), attachment2Name)) .findAny() - .get(); + .orElseThrow(); final byte[] actual2 = attachmentFiles.get(attachment2.getSource()); diff --git a/allure-java-commons/src/test/java/io/qameta/allure/StepLifecycleListenerTest.java b/allure-java-commons/src/test/java/io/qameta/allure/StepLifecycleListenerTest.java index bd09c693..0a0ba0f7 100644 --- a/allure-java-commons/src/test/java/io/qameta/allure/StepLifecycleListenerTest.java +++ b/allure-java-commons/src/test/java/io/qameta/allure/StepLifecycleListenerTest.java @@ -15,7 +15,6 @@ */ package io.qameta.allure; -import io.qameta.allure.aspects.StepsAspects; import io.qameta.allure.listener.LifecycleNotifier; import io.qameta.allure.listener.StepLifecycleListener; import io.qameta.allure.model.Attachment; @@ -23,17 +22,13 @@ import io.qameta.allure.model.StepResult; import io.qameta.allure.model.TestResult; import io.qameta.allure.test.AllureResults; -import io.qameta.allure.test.AllureResultsWriterStub; +import io.qameta.allure.test.RunUtils; import org.junit.jupiter.api.Test; -import java.util.UUID; +import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Stream; -import static io.qameta.allure.util.ResultsUtils.getStatus; -import static io.qameta.allure.util.ResultsUtils.getStatusDetails; -import static java.util.Collections.emptyList; -import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; /** @@ -133,45 +128,12 @@ public void beforeStepStop(final StepResult result) { } protected AllureResults run(final StepLifecycleListener listener, final String... steps) { - final AllureResultsWriterStub writer = new AllureResultsWriterStub(); - final LifecycleNotifier notifier = new LifecycleNotifier( - emptyList(), - emptyList(), - emptyList(), - singletonList(listener) + return RunUtils.runWithinTestContext( + writer -> new AllureLifecycle( + writer, + new LifecycleNotifier(List.of(), List.of(), List.of(), List.of(listener)) + ), + () -> Stream.of(steps).forEach(step -> Allure.step(step, Status.PASSED)) ); - final AllureLifecycle lifecycle = new AllureLifecycle(writer, notifier); - - final String uuid = UUID.randomUUID().toString(); - final TestResult result = new TestResult().setUuid(uuid); - - final AllureLifecycle cached = Allure.getLifecycle(); - try { - Allure.setLifecycle(lifecycle); - StepsAspects.setLifecycle(lifecycle); - - lifecycle.scheduleTestCase(result); - lifecycle.startTestCase(uuid); - - Stream.of(steps).forEach(step -> { - final String stepUuid = UUID.randomUUID().toString(); - lifecycle.startStep(stepUuid, new StepResult().setName(step).setStatus(Status.PASSED)); - lifecycle.stopStep(stepUuid); - }); - } catch (Throwable e) { - lifecycle.updateTestCase(uuid, testResult -> { - getStatus(e).ifPresent(testResult::setStatus); - getStatusDetails(e).ifPresent(testResult::setStatusDetails); - - }); - } finally { - lifecycle.stopTestCase(uuid); - lifecycle.writeTestCase(uuid); - - Allure.setLifecycle(cached); - StepsAspects.setLifecycle(cached); - } - - return writer; } } diff --git a/allure-jbehave/src/test/java/io/qameta/allure/jbehave/AllureJbehaveTest.java b/allure-jbehave/src/test/java/io/qameta/allure/jbehave/AllureJbehaveTest.java index 5e7035e4..1f1917b9 100644 --- a/allure-jbehave/src/test/java/io/qameta/allure/jbehave/AllureJbehaveTest.java +++ b/allure-jbehave/src/test/java/io/qameta/allure/jbehave/AllureJbehaveTest.java @@ -15,8 +15,6 @@ */ package io.qameta.allure.jbehave; -import io.qameta.allure.Allure; -import io.qameta.allure.AllureLifecycle; import io.qameta.allure.Issue; import io.qameta.allure.jbehave.samples.BrokenStorySteps; import io.qameta.allure.jbehave.samples.SimpleStorySteps; @@ -27,7 +25,7 @@ import io.qameta.allure.model.StepResult; import io.qameta.allure.model.TestResult; import io.qameta.allure.test.AllureResults; -import io.qameta.allure.test.AllureResultsWriterStub; +import io.qameta.allure.test.RunUtils; import org.jbehave.core.configuration.MostUsefulConfiguration; import org.jbehave.core.embedder.Embedder; import org.jbehave.core.embedder.EmbedderControls; @@ -176,7 +174,7 @@ void shouldSetDescription() { final AllureResults results = runStories("stories/description.story"); final String expected = "This is description for current story.\n" - + "It should appear on each scenario in report"; + + "It should appear on each scenario in report"; assertThat(results.getTestResults()) .extracting(TestResult::getDescription) @@ -267,42 +265,34 @@ void shouldNotFailIfGivenStoriesSpecified() { } private AllureResults runStories(final String... storyResources) { - final AllureResultsWriterStub writer = new AllureResultsWriterStub(); - final AllureLifecycle lifecycle = new AllureLifecycle(writer); - - final Embedder embedder = new Embedder(); - embedder.useEmbedderMonitor(new NullEmbedderMonitor()); - embedder.useEmbedderControls(new EmbedderControls() - .doGenerateViewAfterStories(false) - .doFailOnStoryTimeout(false) - .doBatch(false) - .doIgnoreFailureInStories(true) - .doIgnoreFailureInView(true) - .doVerboseFailures(false) - .doVerboseFiltering(false) - ); - final AllureJbehave allureJbehave = new AllureJbehave(lifecycle); - embedder.useConfiguration(new MostUsefulConfiguration() - .useStoryLoader(new LoadFromClasspath(this.getClass())) - .useStoryReporterBuilder(new ReportlessStoryReporterBuilder(temp.toFile()) - .withReporters(allureJbehave) - ) - .useDefaultStoryReporter(new NullStoryReporter()) - ); - final InjectableStepsFactory stepsFactory = new InstanceStepsFactory( - embedder.configuration(), - new SimpleStorySteps(), - new BrokenStorySteps() - ); - embedder.useCandidateSteps(stepsFactory.createCandidateSteps()); - final AllureLifecycle cached = Allure.getLifecycle(); - try { - Allure.setLifecycle(lifecycle); + return RunUtils.runTests(lifecycle -> { + final Embedder embedder = new Embedder(); + embedder.useEmbedderMonitor(new NullEmbedderMonitor()); + embedder.useEmbedderControls(new EmbedderControls() + .doGenerateViewAfterStories(false) + .doFailOnStoryTimeout(false) + .doBatch(false) + .doIgnoreFailureInStories(true) + .doIgnoreFailureInView(true) + .doVerboseFailures(false) + .doVerboseFiltering(false) + ); + final AllureJbehave allureJbehave = new AllureJbehave(lifecycle); + embedder.useConfiguration(new MostUsefulConfiguration() + .useStoryLoader(new LoadFromClasspath(this.getClass())) + .useStoryReporterBuilder(new ReportlessStoryReporterBuilder(temp.toFile()) + .withReporters(allureJbehave) + ) + .useDefaultStoryReporter(new NullStoryReporter()) + ); + final InjectableStepsFactory stepsFactory = new InstanceStepsFactory( + embedder.configuration(), + new SimpleStorySteps(), + new BrokenStorySteps() + ); + embedder.useCandidateSteps(stepsFactory.createCandidateSteps()); embedder.runStoriesAsPaths(Arrays.asList(storyResources)); - } finally { - Allure.setLifecycle(cached); - } - return writer; + }); } static class ReportlessStoryReporterBuilder extends StoryReporterBuilder { diff --git a/allure-jbehave5/src/test/java/io/qameta/allure/jbehave5/AllureJbehave5Test.java b/allure-jbehave5/src/test/java/io/qameta/allure/jbehave5/AllureJbehave5Test.java index eb670bab..387f9455 100644 --- a/allure-jbehave5/src/test/java/io/qameta/allure/jbehave5/AllureJbehave5Test.java +++ b/allure-jbehave5/src/test/java/io/qameta/allure/jbehave5/AllureJbehave5Test.java @@ -15,8 +15,6 @@ */ package io.qameta.allure.jbehave5; -import io.qameta.allure.Allure; -import io.qameta.allure.AllureLifecycle; import io.qameta.allure.Issue; import io.qameta.allure.jbehave5.samples.BrokenStorySteps; import io.qameta.allure.jbehave5.samples.SimpleStorySteps; @@ -27,7 +25,7 @@ import io.qameta.allure.model.StepResult; import io.qameta.allure.model.TestResult; import io.qameta.allure.test.AllureResults; -import io.qameta.allure.test.AllureResultsWriterStub; +import io.qameta.allure.test.RunUtils; import org.jbehave.core.configuration.MostUsefulConfiguration; import org.jbehave.core.embedder.Embedder; import org.jbehave.core.embedder.EmbedderControls; @@ -172,7 +170,7 @@ void shouldSetDescription() { final AllureResults results = runStories("stories/description.story"); final String expected = "This is description for current story.\n" - + "It should appear on each scenario in report"; + + "It should appear on each scenario in report"; assertThat(results.getTestResults()) .extracting(TestResult::getDescription) @@ -262,42 +260,34 @@ void shouldNotFailIfGivenStoriesSpecified() { } private AllureResults runStories(final String... storyResources) { - final AllureResultsWriterStub writer = new AllureResultsWriterStub(); - final AllureLifecycle lifecycle = new AllureLifecycle(writer); - - final Embedder embedder = new Embedder(); - embedder.useEmbedderMonitor(new NullEmbedderMonitor()); - embedder.useEmbedderControls(new EmbedderControls() - .doGenerateViewAfterStories(false) - .doFailOnStoryTimeout(false) - .doBatch(false) - .doIgnoreFailureInStories(true) - .doIgnoreFailureInView(true) - .doVerboseFailures(false) - .doVerboseFiltering(false) - ); - final AllureJbehave5 allureJbehave5 = new AllureJbehave5(lifecycle); - embedder.useConfiguration(new MostUsefulConfiguration() - .useStoryLoader(new LoadFromClasspath(this.getClass())) - .useStoryReporterBuilder(new ReportlessStoryReporterBuilder(temp.toFile()) - .withReporters(allureJbehave5) - ) - .useDefaultStoryReporter(new NullStoryReporter()) - ); - final InjectableStepsFactory stepsFactory = new InstanceStepsFactory( - embedder.configuration(), - new SimpleStorySteps(), - new BrokenStorySteps() - ); - embedder.useStepsFactory(stepsFactory); - final AllureLifecycle cached = Allure.getLifecycle(); - try { - Allure.setLifecycle(lifecycle); + return RunUtils.runTests(lifecycle -> { + final Embedder embedder = new Embedder(); + embedder.useEmbedderMonitor(new NullEmbedderMonitor()); + embedder.useEmbedderControls(new EmbedderControls() + .doGenerateViewAfterStories(false) + .doFailOnStoryTimeout(false) + .doBatch(false) + .doIgnoreFailureInStories(true) + .doIgnoreFailureInView(true) + .doVerboseFailures(false) + .doVerboseFiltering(false) + ); + final AllureJbehave5 allureJbehave5 = new AllureJbehave5(lifecycle); + embedder.useConfiguration(new MostUsefulConfiguration() + .useStoryLoader(new LoadFromClasspath(this.getClass())) + .useStoryReporterBuilder(new ReportlessStoryReporterBuilder(temp.toFile()) + .withReporters(allureJbehave5) + ) + .useDefaultStoryReporter(new NullStoryReporter()) + ); + final InjectableStepsFactory stepsFactory = new InstanceStepsFactory( + embedder.configuration(), + new SimpleStorySteps(), + new BrokenStorySteps() + ); + embedder.useStepsFactory(stepsFactory); embedder.runStoriesAsPaths(Arrays.asList(storyResources)); - } finally { - Allure.setLifecycle(cached); - } - return writer; + }); } static class ReportlessStoryReporterBuilder extends StoryReporterBuilder { diff --git a/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/AllureJunitPlatformTest.java b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/AllureJunitPlatformTest.java index 33be2145..ceccd76b 100644 --- a/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/AllureJunitPlatformTest.java +++ b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/AllureJunitPlatformTest.java @@ -16,11 +16,7 @@ package io.qameta.allure.junitplatform; import io.github.glytching.junit.extension.system.SystemProperty; -import io.qameta.allure.Allure; -import io.qameta.allure.AllureLifecycle; import io.qameta.allure.Issue; -import io.qameta.allure.aspects.AttachmentsAspects; -import io.qameta.allure.aspects.StepsAspects; import io.qameta.allure.junitplatform.features.AllureIdAnnotationSupport; import io.qameta.allure.junitplatform.features.BrokenInAfterAllTests; import io.qameta.allure.junitplatform.features.BrokenInBeforeAllTests; @@ -67,7 +63,7 @@ import io.qameta.allure.model.TestResult; import io.qameta.allure.test.AllureFeatures; import io.qameta.allure.test.AllureResults; -import io.qameta.allure.test.AllureResultsWriterStub; +import io.qameta.allure.test.RunUtils; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.parallel.ResourceLock; @@ -841,35 +837,23 @@ void shouldProcessAllureParameterReportingEvents() { @Test @AllureFeatures.Retries void shouldSetDifferentUuidsInDifferentRuns() { - final AllureResultsWriterStub results = new AllureResultsWriterStub(); - final AllureLifecycle lifecycle = new AllureLifecycle(results); - - final LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() - .filters(new AllurePostDiscoveryFilter(null)) - .selectors(DiscoverySelectors.selectClass(OneTest.class)) - .build(); - - final LauncherConfig config = LauncherConfig.builder() - .enableTestExecutionListenerAutoRegistration(false) - .addTestExecutionListeners(new AllureJunitPlatform(lifecycle)) - .enablePostDiscoveryFilterAutoRegistration(false) - .build(); - final Launcher launcher = LauncherFactory.create(config); - - final AllureLifecycle defaultLifecycle = Allure.getLifecycle(); - try { - Allure.setLifecycle(lifecycle); - StepsAspects.setLifecycle(lifecycle); - AttachmentsAspects.setLifecycle(lifecycle); + final AllureResults results = RunUtils.runTests(lifecycle -> { + final LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() + .filters(new AllurePostDiscoveryFilter(null)) + .selectors(DiscoverySelectors.selectClass(OneTest.class)) + .build(); + + final LauncherConfig config = LauncherConfig.builder() + .enableTestExecutionListenerAutoRegistration(false) + .addTestExecutionListeners(new AllureJunitPlatform(lifecycle)) + .enablePostDiscoveryFilterAutoRegistration(false) + .build(); + final Launcher launcher = LauncherFactory.create(config); // execute request twice launcher.execute(request); launcher.execute(request); - } finally { - Allure.setLifecycle(defaultLifecycle); - StepsAspects.setLifecycle(defaultLifecycle); - AttachmentsAspects.setLifecycle(defaultLifecycle); - } + }); final List testResults = results.getTestResults(); @@ -888,4 +872,5 @@ void shouldSetDifferentUuidsInDifferentRuns() { .isEqualTo(tr2.getHistoryId()); } + } diff --git a/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/AllureJunitPlatformTestUtils.java b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/AllureJunitPlatformTestUtils.java index df27d2f3..9cd8a8f6 100644 --- a/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/AllureJunitPlatformTestUtils.java +++ b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/AllureJunitPlatformTestUtils.java @@ -15,13 +15,9 @@ */ package io.qameta.allure.junitplatform; -import io.qameta.allure.Allure; -import io.qameta.allure.AllureLifecycle; import io.qameta.allure.Step; -import io.qameta.allure.aspects.AttachmentsAspects; -import io.qameta.allure.aspects.StepsAspects; import io.qameta.allure.test.AllureResults; -import io.qameta.allure.test.AllureResultsWriterStub; +import io.qameta.allure.test.RunUtils; import io.qameta.allure.testfilter.TestPlan; import org.junit.platform.engine.discovery.ClassSelector; import org.junit.platform.engine.discovery.DiscoverySelectors; @@ -49,58 +45,24 @@ public static AllureResults runClasses(final Class... classes) { @Step("Run classes {classes}") public static AllureResults runClasses(final TestPlan testPlan, final Class... classes) { - final AllureResultsWriterStub writerStub = new AllureResultsWriterStub(); - final AllureLifecycle lifecycle = new AllureLifecycle(writerStub); + return RunUtils.runTests(lifecycle -> { + final ClassSelector[] classSelectors = Stream.of(classes) + .map(DiscoverySelectors::selectClass) + .toArray(ClassSelector[]::new); - final ClassSelector[] classSelectors = Stream.of(classes) - .map(DiscoverySelectors::selectClass) - .toArray(ClassSelector[]::new); + final LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() + .filters(new AllurePostDiscoveryFilter(testPlan)) + .selectors(classSelectors) + .build(); - final LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() - .filters(new AllurePostDiscoveryFilter(testPlan)) - .selectors(classSelectors) - .build(); - - final LauncherConfig config = LauncherConfig.builder() - .enableTestExecutionListenerAutoRegistration(false) - .addTestExecutionListeners(new AllureJunitPlatform(lifecycle)) - .enablePostDiscoveryFilterAutoRegistration(false) - .build(); - final Launcher launcher = LauncherFactory.create(config); - - final AllureLifecycle defaultLifecycle = Allure.getLifecycle(); - try { - Allure.setLifecycle(lifecycle); - StepsAspects.setLifecycle(lifecycle); - AttachmentsAspects.setLifecycle(lifecycle); + final LauncherConfig config = LauncherConfig.builder() + .enableTestExecutionListenerAutoRegistration(false) + .addTestExecutionListeners(new AllureJunitPlatform(lifecycle)) + .enablePostDiscoveryFilterAutoRegistration(false) + .build(); + final Launcher launcher = LauncherFactory.create(config); launcher.execute(request); - return writerStub; - } finally { - Allure.setLifecycle(defaultLifecycle); - StepsAspects.setLifecycle(defaultLifecycle); - AttachmentsAspects.setLifecycle(defaultLifecycle); - } + }); } - @Step("Build test plan for {classes}") - public static org.junit.platform.launcher.TestPlan buildPlan( - final io.qameta.allure.testfilter.TestPlan testPlan, - final Class... classes) { - final ClassSelector[] classSelectors = Stream.of(classes) - .map(DiscoverySelectors::selectClass) - .toArray(ClassSelector[]::new); - - final LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() - .selectors(classSelectors) - .filters(new AllurePostDiscoveryFilter(testPlan)) - .build(); - - final LauncherConfig config = LauncherConfig.builder() - .enableTestExecutionListenerAutoRegistration(false) - .enablePostDiscoveryFilterAutoRegistration(false) - .build(); - final Launcher launcher = LauncherFactory.create(config); - - return launcher.discover(request); - } } diff --git a/allure-junit4/src/test/java/io/qameta/allure/junit4/AllureJunit4Test.java b/allure-junit4/src/test/java/io/qameta/allure/junit4/AllureJunit4Test.java index 011443e0..99d08a64 100644 --- a/allure-junit4/src/test/java/io/qameta/allure/junit4/AllureJunit4Test.java +++ b/allure-junit4/src/test/java/io/qameta/allure/junit4/AllureJunit4Test.java @@ -15,11 +15,7 @@ */ package io.qameta.allure.junit4; -import io.qameta.allure.Allure; -import io.qameta.allure.AllureLifecycle; import io.qameta.allure.Step; -import io.qameta.allure.aspects.AttachmentsAspects; -import io.qameta.allure.aspects.StepsAspects; import io.qameta.allure.junit4.samples.AssumptionFailedTest; import io.qameta.allure.junit4.samples.BrokenTest; import io.qameta.allure.junit4.samples.BrokenWithoutMessageTest; @@ -44,7 +40,7 @@ import io.qameta.allure.model.TestResult; import io.qameta.allure.test.AllureFeatures; import io.qameta.allure.test.AllureResults; -import io.qameta.allure.test.AllureResultsWriterStub; +import io.qameta.allure.test.RunUtils; import io.qameta.allure.testfilter.TestPlan; import io.qameta.allure.testfilter.TestPlanV1_0; import org.junit.jupiter.api.Test; @@ -422,26 +418,15 @@ private AllureResults runClasses(final Class... classes) { @Step("Run classes {classes}") private AllureResults runClasses(final TestPlan testPlan, final Class... classes) { - final AllureResultsWriterStub writerStub = new AllureResultsWriterStub(); - final AllureLifecycle lifecycle = new AllureLifecycle(writerStub); - final JUnitCore core = new JUnitCore(); - core.addListener(new AllureJunit4(lifecycle)); - - final Request request = Request - .classes(classes) - .filterWith(new AllureJunit4Filter(testPlan)); - - final AllureLifecycle defaultLifecycle = Allure.getLifecycle(); - try { - Allure.setLifecycle(lifecycle); - StepsAspects.setLifecycle(lifecycle); - AttachmentsAspects.setLifecycle(lifecycle); + return RunUtils.runTests(lifecycle -> { + final JUnitCore core = new JUnitCore(); + core.addListener(new AllureJunit4(lifecycle)); + + final Request request = Request + .classes(classes) + .filterWith(new AllureJunit4Filter(testPlan)); + core.run(request); - return writerStub; - } finally { - Allure.setLifecycle(defaultLifecycle); - StepsAspects.setLifecycle(defaultLifecycle); - AttachmentsAspects.setLifecycle(defaultLifecycle); - } + }); } } diff --git a/allure-junit5/src/test/java/io/qameta/allure/junit5/AllureJunit5Test.java b/allure-junit5/src/test/java/io/qameta/allure/junit5/AllureJunit5Test.java index 09e15eb3..19e62a91 100644 --- a/allure-junit5/src/test/java/io/qameta/allure/junit5/AllureJunit5Test.java +++ b/allure-junit5/src/test/java/io/qameta/allure/junit5/AllureJunit5Test.java @@ -15,12 +15,8 @@ */ package io.qameta.allure.junit5; -import io.qameta.allure.Allure; -import io.qameta.allure.AllureLifecycle; import io.qameta.allure.Issue; import io.qameta.allure.Step; -import io.qameta.allure.aspects.AttachmentsAspects; -import io.qameta.allure.aspects.StepsAspects; import io.qameta.allure.junit5.features.AfterEachFixtureBrokenSupport; import io.qameta.allure.junit5.features.AllFixtureSupport; import io.qameta.allure.junit5.features.BeforeAllFixtureFailureSupport; @@ -39,7 +35,7 @@ import io.qameta.allure.model.TestResultContainer; import io.qameta.allure.test.AllureFeatures; import io.qameta.allure.test.AllureResults; -import io.qameta.allure.test.AllureResultsWriterStub; +import io.qameta.allure.test.RunUtils; import org.junit.jupiter.api.Test; import org.junit.platform.engine.discovery.ClassSelector; import org.junit.platform.engine.discovery.DiscoverySelectors; @@ -415,35 +411,22 @@ void shouldSupportFailureInBeforeAllFixture() { @Step("Run classes {classes}") private AllureResults runClasses(final Class... classes) { - final AllureResultsWriterStub writerStub = new AllureResultsWriterStub(); - final AllureLifecycle lifecycle = new AllureLifecycle(writerStub); - - final ClassSelector[] classSelectors = Stream.of(classes) - .map(DiscoverySelectors::selectClass) - .toArray(ClassSelector[]::new); - - final LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() - .configurationParameter("junit.jupiter.extensions.autodetection.enabled", "true") - .selectors(classSelectors) - .build(); - - final LauncherConfig config = LauncherConfig.builder() - .enableTestExecutionListenerAutoRegistration(false) - .addTestExecutionListeners(new AllureJunitPlatform(lifecycle)) - .build(); - final Launcher launcher = LauncherFactory.create(config); - - final AllureLifecycle defaultLifecycle = Allure.getLifecycle(); - try { - Allure.setLifecycle(lifecycle); - StepsAspects.setLifecycle(lifecycle); - AttachmentsAspects.setLifecycle(lifecycle); + return RunUtils.runTests(lifecycle -> { + final ClassSelector[] classSelectors = Stream.of(classes) + .map(DiscoverySelectors::selectClass) + .toArray(ClassSelector[]::new); + + final LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() + .configurationParameter("junit.jupiter.extensions.autodetection.enabled", "true") + .selectors(classSelectors) + .build(); + + final LauncherConfig config = LauncherConfig.builder() + .enableTestExecutionListenerAutoRegistration(false) + .addTestExecutionListeners(new AllureJunitPlatform(lifecycle)) + .build(); + final Launcher launcher = LauncherFactory.create(config); launcher.execute(request); - return writerStub; - } finally { - Allure.setLifecycle(defaultLifecycle); - StepsAspects.setLifecycle(defaultLifecycle); - AttachmentsAspects.setLifecycle(defaultLifecycle); - } + }); } } diff --git a/allure-karate/src/test/java/io/qameta/allure/karate/TestRunner.java b/allure-karate/src/test/java/io/qameta/allure/karate/TestRunner.java index 0e53fbf8..a8300251 100644 --- a/allure-karate/src/test/java/io/qameta/allure/karate/TestRunner.java +++ b/allure-karate/src/test/java/io/qameta/allure/karate/TestRunner.java @@ -15,12 +15,8 @@ */ package io.qameta.allure.karate; -import io.qameta.allure.Allure; -import io.qameta.allure.AllureLifecycle; -import io.qameta.allure.aspects.AttachmentsAspects; -import io.qameta.allure.aspects.StepsAspects; import io.qameta.allure.test.AllureResults; -import io.qameta.allure.test.AllureResultsWriterStub; +import io.qameta.allure.test.RunUtils; import org.mockserver.client.MockServerClient; import org.mockserver.model.HttpRequest; import org.mockserver.model.HttpResponse; @@ -59,36 +55,26 @@ AllureResults runApi(final String... featurePath) { } AllureResults run(final String... path) { - final AllureResultsWriterStub writerStub = new AllureResultsWriterStub(); - final AllureLifecycle lifecycle = new AllureLifecycle(writerStub); - final AllureKarate allureKarate = new AllureKarate(lifecycle); + return RunUtils.runTests(lifecycle -> { + final AllureKarate allureKarate = new AllureKarate(lifecycle); - final AllureLifecycle defaultLifecycle = Allure.getLifecycle(); - try { - Allure.setLifecycle(lifecycle); - StepsAspects.setLifecycle(lifecycle); - AttachmentsAspects.setLifecycle(lifecycle); - - com.intuit.karate.Runner.builder() - .path(path) - .hook(allureKarate) - .backupReportDir(false) - .outputJunitXml(false) - .outputCucumberJson(false) - .outputHtmlReport(false) - .parallel(1); - - return writerStub; - } finally { - Allure.setLifecycle(defaultLifecycle); - StepsAspects.setLifecycle(defaultLifecycle); - AttachmentsAspects.setLifecycle(defaultLifecycle); - if (server != null && server.isRunning()) { - server.stop(); - } - if (client != null && !client.hasStopped()) { - client.stop(); + try { + com.intuit.karate.Runner.builder() + .path(path) + .hook(allureKarate) + .backupReportDir(false) + .outputJunitXml(false) + .outputCucumberJson(false) + .outputHtmlReport(false) + .parallel(1); + } finally { + if (server != null && server.isRunning()) { + server.stop(); + } + if (client != null && !client.hasStopped()) { + client.stop(); + } } - } + }); } } diff --git a/allure-spock/src/test/groovy/io/qameta/allure/spock/AllureSpockTest.java b/allure-spock/src/test/groovy/io/qameta/allure/spock/AllureSpockTest.java index afb51f3e..f7bc5af8 100644 --- a/allure-spock/src/test/groovy/io/qameta/allure/spock/AllureSpockTest.java +++ b/allure-spock/src/test/groovy/io/qameta/allure/spock/AllureSpockTest.java @@ -15,10 +15,6 @@ */ package io.qameta.allure.spock; -import io.qameta.allure.Allure; -import io.qameta.allure.AllureLifecycle; -import io.qameta.allure.aspects.AttachmentsAspects; -import io.qameta.allure.aspects.StepsAspects; import io.qameta.allure.model.Label; import io.qameta.allure.model.Link; import io.qameta.allure.model.Parameter; @@ -37,7 +33,7 @@ import io.qameta.allure.spock.samples.TestWithCustomAnnotations; import io.qameta.allure.spock.samples.TestWithSteps; import io.qameta.allure.test.AllureResults; -import io.qameta.allure.test.AllureResultsWriterStub; +import io.qameta.allure.test.RunUtils; import org.junit.jupiter.api.Test; import org.junit.runner.notification.RunNotifier; import org.spockframework.runtime.JUnitDescriptionGenerator; @@ -225,32 +221,16 @@ void shouldSupportDataDrivenTests() { } protected AllureResults run(final Class clazz) { - final AllureResultsWriterStub results = new AllureResultsWriterStub(); - final AllureLifecycle lifecycle = new AllureLifecycle(results); + return RunUtils.runTests(lifecycle -> { + final RunNotifier notifier = new RunNotifier(); + final SpecInfo spec = new SpecInfoBuilder(clazz).build(); + spec.addListener(new AllureSpock(lifecycle)); - final RunNotifier notifier = new RunNotifier(); - final SpecInfo spec = new SpecInfoBuilder(clazz).build(); - spec.addListener(new AllureSpock(lifecycle)); - - new JUnitDescriptionGenerator(spec).describeSpecMethods(); - new JUnitDescriptionGenerator(spec).describeSpec(); - - final AllureLifecycle cached = Allure.getLifecycle(); - try { - Allure.setLifecycle(lifecycle); - StepsAspects.setLifecycle(lifecycle); - AttachmentsAspects.setLifecycle(lifecycle); + new JUnitDescriptionGenerator(spec).describeSpecMethods(); + new JUnitDescriptionGenerator(spec).describeSpec(); RunContext.get().createSpecRunner(spec, notifier).run(); - } catch (Exception e) { - throw new RuntimeException("could not execute sample", e); - } finally { - Allure.setLifecycle(cached); - StepsAspects.setLifecycle(cached); - AttachmentsAspects.setLifecycle(cached); - } - - return results; + }); } private static Predicate mutedPredicate() { diff --git a/allure-spock2/src/main/java/io/qameta/allure/spock2/AllureSpock2.java b/allure-spock2/src/main/java/io/qameta/allure/spock2/AllureSpock2.java index a07a47f0..c0ae1667 100644 --- a/allure-spock2/src/main/java/io/qameta/allure/spock2/AllureSpock2.java +++ b/allure-spock2/src/main/java/io/qameta/allure/spock2/AllureSpock2.java @@ -411,7 +411,7 @@ public void intercept(final IMethodInvocation invocation) throws Throwable { .setStatus(getStatus(throwable).orElse(Status.BROKEN)) .setStatusDetails(getStatusDetails(throwable).orElse(null)) ); - ExceptionUtils.sneakyThrow(throwable); + throw ExceptionUtils.sneakyThrow(throwable); } finally { getLifecycle().stopFixture(fixtureUuid); } diff --git a/allure-spock2/src/test/groovy/io/qameta/allure/spock2/AllureSpock2Test.java b/allure-spock2/src/test/groovy/io/qameta/allure/spock2/AllureSpock2Test.java index 402a77ee..c5c55f79 100644 --- a/allure-spock2/src/test/groovy/io/qameta/allure/spock2/AllureSpock2Test.java +++ b/allure-spock2/src/test/groovy/io/qameta/allure/spock2/AllureSpock2Test.java @@ -15,11 +15,7 @@ */ package io.qameta.allure.spock2; -import io.qameta.allure.Allure; -import io.qameta.allure.AllureLifecycle; import io.qameta.allure.Step; -import io.qameta.allure.aspects.AttachmentsAspects; -import io.qameta.allure.aspects.StepsAspects; import io.qameta.allure.model.ExecutableItem; import io.qameta.allure.model.FixtureResult; import io.qameta.allure.model.Label; @@ -48,7 +44,7 @@ import io.qameta.allure.spock2.samples.TestsWithIdForFilter; import io.qameta.allure.test.AllureFeatures; import io.qameta.allure.test.AllureResults; -import io.qameta.allure.test.AllureResultsWriterStub; +import io.qameta.allure.test.RunUtils; import io.qameta.allure.testfilter.TestPlan; import io.qameta.allure.testfilter.TestPlanV1_0; import org.apache.commons.lang3.tuple.Pair; @@ -607,46 +603,33 @@ public static AllureResults runClasses(final Class... classes) { @Step("Run classes {classes}") public static AllureResults runClasses(final TestPlan testPlan, final Class... classes) { - final AllureResultsWriterStub writerStub = new AllureResultsWriterStub(); - final AllureLifecycle lifecycle = new AllureLifecycle(writerStub); - - final ClassSelector[] classSelectors = Stream.of(classes) - .map(DiscoverySelectors::selectClass) - .toArray(ClassSelector[]::new); - - final LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() - .selectors(classSelectors) - .build(); - - final RunContext context = RunContext.get(); - final GlobalExtensionRegistry extensionRegistry = ReflectionUtils - .tryToReadFieldValue(RunContext.class, "globalExtensionRegistry", context) - .andThenTry(GlobalExtensionRegistry.class::cast) - .toOptional() - .orElseThrow(() -> new AssertionError("could not access globalExtensionRegistry field of RunContext")); - extensionRegistry.getGlobalExtensions().add(new AllureSpock2(lifecycle, testPlan)); - - final LauncherConfig config = LauncherConfig.builder() - .enableTestEngineAutoRegistration(false) - .addTestEngines(new SpockEngine()) - .enableTestExecutionListenerAutoRegistration(false) - .enablePostDiscoveryFilterAutoRegistration(false) - .build(); - - final Launcher launcher = LauncherFactory.create(config); - - final AllureLifecycle defaultLifecycle = Allure.getLifecycle(); - try { - Allure.setLifecycle(lifecycle); - StepsAspects.setLifecycle(lifecycle); - AttachmentsAspects.setLifecycle(lifecycle); + return RunUtils.runTests(lifecycle -> { + final ClassSelector[] classSelectors = Stream.of(classes) + .map(DiscoverySelectors::selectClass) + .toArray(ClassSelector[]::new); + + final LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() + .selectors(classSelectors) + .build(); + + final RunContext context = RunContext.get(); + final GlobalExtensionRegistry extensionRegistry = ReflectionUtils + .tryToReadFieldValue(RunContext.class, "globalExtensionRegistry", context) + .andThenTry(GlobalExtensionRegistry.class::cast) + .toOptional() + .orElseThrow(() -> new AssertionError("could not access globalExtensionRegistry field of RunContext")); + extensionRegistry.getGlobalExtensions().add(new AllureSpock2(lifecycle, testPlan)); + + final LauncherConfig config = LauncherConfig.builder() + .enableTestEngineAutoRegistration(false) + .addTestEngines(new SpockEngine()) + .enableTestExecutionListenerAutoRegistration(false) + .enablePostDiscoveryFilterAutoRegistration(false) + .build(); + + final Launcher launcher = LauncherFactory.create(config); launcher.execute(request); - return writerStub; - } finally { - Allure.setLifecycle(defaultLifecycle); - StepsAspects.setLifecycle(defaultLifecycle); - AttachmentsAspects.setLifecycle(defaultLifecycle); - } + }); } private String printSteps(final ExecutableItem item) { diff --git a/allure-testng/src/test/java/io/qameta/allure/testng/AllureTestNgTest.java b/allure-testng/src/test/java/io/qameta/allure/testng/AllureTestNgTest.java index 73656472..7af9c530 100644 --- a/allure-testng/src/test/java/io/qameta/allure/testng/AllureTestNgTest.java +++ b/allure-testng/src/test/java/io/qameta/allure/testng/AllureTestNgTest.java @@ -15,12 +15,8 @@ */ package io.qameta.allure.testng; -import io.qameta.allure.Allure; -import io.qameta.allure.AllureLifecycle; import io.qameta.allure.Issue; import io.qameta.allure.Step; -import io.qameta.allure.aspects.AttachmentsAspects; -import io.qameta.allure.aspects.StepsAspects; import io.qameta.allure.model.Attachment; import io.qameta.allure.model.FixtureResult; import io.qameta.allure.model.Label; @@ -35,7 +31,7 @@ import io.qameta.allure.model.WithSteps; import io.qameta.allure.test.AllureFeatures; import io.qameta.allure.test.AllureResults; -import io.qameta.allure.test.AllureResultsWriterStub; +import io.qameta.allure.test.RunUtils; import io.qameta.allure.testfilter.TestPlan; import io.qameta.allure.testfilter.TestPlanV1_0; import io.qameta.allure.testng.config.AllureTestNgConfig; @@ -180,7 +176,7 @@ public void testWithTimeout(final XmlSuite.ParallelMode mode, final int threadCo .hasSize(2) .as("Unexpectedly passed status or stage of tests") .allMatch(testResult -> testResult.getStatus().equals(Status.PASSED) && - testResult.getStage().equals(Stage.FINISHED)) + testResult.getStage().equals(Stage.FINISHED)) .extracting(TestResult::getName) .as("Unexpectedly passed name of tests") .containsOnlyElementsOf(asList( @@ -1106,7 +1102,7 @@ public void shouldDisplayDisabledTests() { public void shouldNotDisplayDisabledTests() { AllureTestNgConfig allureTestNgConfig = AllureTestNgConfig.loadConfigProperties(); allureTestNgConfig.setHideDisabledTests(true); - final AllureResults results = runTestNgSuites(allureTestNgConfig,"suites/gh-369.xml"); + final AllureResults results = runTestNgSuites(allureTestNgConfig, "suites/gh-369.xml"); assertThat(results.getTestResults()) .extracting(TestResult::getName, TestResult::getStatus) .containsOnly(tuple("enabled", Status.PASSED)); @@ -1253,7 +1249,8 @@ private AllureResults runTestNgSuites(AllureTestNgConfig config, final String... } private AllureResults runTestNgSuites(final Consumer configurer, - final String... suites) {; + final String... suites) { + ; return runTestNgSuites(configurer, AllureTestNgConfig.loadConfigProperties(), suites); } @@ -1272,29 +1269,17 @@ private AllureResults runTestNgSuites(final Consumer configurer, .as("Cannot find all suite xml files") .hasSameSizeAs(suiteFiles); - final AllureResultsWriterStub results = new AllureResultsWriterStub(); - final AllureLifecycle lifecycle = new AllureLifecycle(results); - final AllureTestNg adapter = new AllureTestNg(lifecycle, - new AllureTestNgTestFilter(), - config); - final TestNG testNg = new TestNG(false); - testNg.addListener((ITestNGListener) adapter); - testNg.setTestSuites(suiteFiles); - - configurer.accept(testNg); + return RunUtils.runTests(lifecycle -> { + final AllureTestNg adapter = new AllureTestNg(lifecycle, + new AllureTestNgTestFilter(), + config); + final TestNG testNg = new TestNG(false); + testNg.addListener((ITestNGListener) adapter); + testNg.setTestSuites(suiteFiles); - final AllureLifecycle cached = Allure.getLifecycle(); - try { - Allure.setLifecycle(lifecycle); - StepsAspects.setLifecycle(lifecycle); - AttachmentsAspects.setLifecycle(lifecycle); + configurer.accept(testNg); testNg.run(); - } finally { - Allure.setLifecycle(cached); - StepsAspects.setLifecycle(cached); - AttachmentsAspects.setLifecycle(cached); - } - return results; + }); } protected Consumer parallel(final XmlSuite.ParallelMode mode, @@ -1329,13 +1314,13 @@ private Predicate hasLinks() { @Step("Find flaky") private Predicate flakyPredicate() { return testResult -> Objects.nonNull(testResult.getStatusDetails()) - && testResult.getStatusDetails().isFlaky(); + && testResult.getStatusDetails().isFlaky(); } @Step("Find muted") private Predicate mutedPredicate() { return testResult -> Objects.nonNull(testResult.getStatusDetails()) - && testResult.getStatusDetails().isMuted(); + && testResult.getStatusDetails().isMuted(); } @Step("Get uuids by container name") @@ -1517,28 +1502,15 @@ public void correctIdIncorrectSelector() { ); } - public AllureResultsWriterStub runTestPlan(TestPlan plan, final Class... testClasses) { - final AllureResultsWriterStub results = new AllureResultsWriterStub(); - final AllureLifecycle lifecycle = new AllureLifecycle(results); - final AllureTestNg adapter = new AllureTestNg(lifecycle, new AllureTestNgTestFilter(plan)); - TestNG testNG = new TestNG(false); - testNG.addListener((ITestNGListener) adapter); - testNG.setTestClasses(testClasses); - testNG.setOutputDirectory("build/test-output"); - - - final AllureLifecycle cached = Allure.getLifecycle(); - try { - Allure.setLifecycle(lifecycle); - StepsAspects.setLifecycle(lifecycle); - AttachmentsAspects.setLifecycle(lifecycle); + public AllureResults runTestPlan(final TestPlan plan, final Class... testClasses) { + return RunUtils.runTests(lifecycle -> { + final AllureTestNg adapter = new AllureTestNg(lifecycle, new AllureTestNgTestFilter(plan)); + final TestNG testNG = new TestNG(false); + testNG.addListener((ITestNGListener) adapter); + testNG.setTestClasses(testClasses); + testNG.setOutputDirectory("build/test-output"); testNG.run(); - return results; - } finally { - Allure.setLifecycle(cached); - StepsAspects.setLifecycle(cached); - AttachmentsAspects.setLifecycle(cached); - } + }); } private Integer getOrderParameter(final TestResult result) {