|
| 1 | +/* |
| 2 | + * Copyright 2016-2026 Qameta Software Inc |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.qameta.allure.junit5; |
| 17 | + |
| 18 | +import io.qameta.allure.Allure; |
| 19 | +import io.qameta.allure.Param; |
| 20 | +import io.qameta.allure.junitplatform.AllureJunitPlatform; |
| 21 | +import io.qameta.allure.model.FixtureResult; |
| 22 | +import io.qameta.allure.model.Parameter; |
| 23 | +import io.qameta.allure.model.Status; |
| 24 | +import io.qameta.allure.model.StepResult; |
| 25 | +import io.qameta.allure.model.TestResult; |
| 26 | +import io.qameta.allure.model.TestResultContainer; |
| 27 | +import io.qameta.allure.test.AllureResults; |
| 28 | +import io.qameta.allure.test.RunUtils; |
| 29 | +import org.junit.jupiter.api.AfterEach; |
| 30 | +import org.junit.jupiter.api.BeforeEach; |
| 31 | +import org.junit.jupiter.api.Nested; |
| 32 | +import org.junit.jupiter.api.Tag; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 35 | +import org.junit.platform.engine.discovery.ClassSelector; |
| 36 | +import org.junit.platform.engine.discovery.DiscoverySelectors; |
| 37 | +import org.junit.platform.launcher.Launcher; |
| 38 | +import org.junit.platform.launcher.LauncherDiscoveryRequest; |
| 39 | +import org.junit.platform.launcher.core.LauncherConfig; |
| 40 | +import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; |
| 41 | +import org.junit.platform.launcher.core.LauncherFactory; |
| 42 | + |
| 43 | +import java.util.List; |
| 44 | +import java.util.stream.Stream; |
| 45 | + |
| 46 | +import static org.assertj.core.api.Assertions.assertThat; |
| 47 | +import static org.assertj.core.api.Assertions.tuple; |
| 48 | + |
| 49 | +@Tag("junit6-compat") |
| 50 | +@SuppressWarnings("unused") |
| 51 | +class AllureJunit5Junit6CompatibilityTest { |
| 52 | + |
| 53 | + @ExtendWith(AllureJunit5.class) |
| 54 | + static class CompatParametersTest { |
| 55 | + |
| 56 | + @org.junit.jupiter.params.ParameterizedTest |
| 57 | + @org.junit.jupiter.params.provider.ValueSource(strings = {"a", "b"}) |
| 58 | + void paramTest(@Param("id") String value) { |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Nested |
| 63 | + @ExtendWith(AllureJunit5.class) |
| 64 | + class CompatFixtures { |
| 65 | + |
| 66 | + @BeforeEach |
| 67 | + void setUp() { |
| 68 | + Allure.step("before step"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void testBody() { |
| 73 | + Allure.step("test step"); |
| 74 | + } |
| 75 | + |
| 76 | + @AfterEach |
| 77 | + void tearDown() { |
| 78 | + Allure.step("after step"); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void shouldCaptureParametersWithParamAnnotationOnJunit6() { |
| 84 | + AllureResults results = runWithLauncher(CompatParametersTest.class); |
| 85 | + |
| 86 | + assertThat(results.getTestResults()).isNotEmpty(); |
| 87 | + |
| 88 | + List<Parameter> allParams = results.getTestResults().stream() |
| 89 | + .flatMap(tr -> tr.getParameters().stream()) |
| 90 | + .toList(); |
| 91 | + |
| 92 | + assertThat(allParams) |
| 93 | + .isNotEmpty() |
| 94 | + .extracting(Parameter::getName, Parameter::getValue) |
| 95 | + .contains( |
| 96 | + tuple("id", "a"), |
| 97 | + tuple("id", "b") |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void shouldCaptureFixturesAndStepsOnJunit6() { |
| 103 | + AllureResults results = runWithLauncher(CompatFixtures.class); |
| 104 | + |
| 105 | + assertThat(results.getTestResults()).hasSize(1); |
| 106 | + TestResult testResult = results.getTestResults().get(0); |
| 107 | + |
| 108 | + assertThat(results.getTestResultContainers()) |
| 109 | + .flatExtracting(TestResultContainer::getChildren) |
| 110 | + .contains(testResult.getUuid()); |
| 111 | + |
| 112 | + assertThat(results.getTestResultContainers()) |
| 113 | + .flatExtracting(TestResultContainer::getBefores) |
| 114 | + .extracting(FixtureResult::getStatus) |
| 115 | + .contains(Status.PASSED); |
| 116 | + |
| 117 | + assertThat(results.getTestResultContainers()) |
| 118 | + .flatExtracting(TestResultContainer::getAfters) |
| 119 | + .extracting(FixtureResult::getStatus) |
| 120 | + .contains(Status.PASSED); |
| 121 | + |
| 122 | + assertThat(results.getTestResultContainers()) |
| 123 | + .flatExtracting(TestResultContainer::getBefores) |
| 124 | + .flatExtracting(FixtureResult::getSteps) |
| 125 | + .extracting(StepResult::getName) |
| 126 | + .contains("before step"); |
| 127 | + |
| 128 | + assertThat(results.getTestResults()) |
| 129 | + .flatExtracting(TestResult::getSteps) |
| 130 | + .extracting(StepResult::getName) |
| 131 | + .contains("test step"); |
| 132 | + |
| 133 | + assertThat(results.getTestResultContainers()) |
| 134 | + .flatExtracting(TestResultContainer::getAfters) |
| 135 | + .flatExtracting(FixtureResult::getSteps) |
| 136 | + .extracting(StepResult::getName) |
| 137 | + .contains("after step"); |
| 138 | + } |
| 139 | + |
| 140 | + @io.qameta.allure.Step("Run classes {classes}") |
| 141 | + private AllureResults runWithLauncher(Class<?>... classes) { |
| 142 | + return RunUtils.runTests(lifecycle -> { |
| 143 | + ClassSelector[] selectors = Stream.of(classes) |
| 144 | + .map(DiscoverySelectors::selectClass) |
| 145 | + .toArray(ClassSelector[]::new); |
| 146 | + |
| 147 | + LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() |
| 148 | + .configurationParameter("junit.jupiter.extensions.autodetection.enabled", "true") |
| 149 | + .selectors(selectors) |
| 150 | + .build(); |
| 151 | + |
| 152 | + LauncherConfig config = LauncherConfig.builder() |
| 153 | + .enableTestExecutionListenerAutoRegistration(false) |
| 154 | + .addTestExecutionListeners(new AllureJunitPlatform(lifecycle)) |
| 155 | + .build(); |
| 156 | + |
| 157 | + Launcher launcher = LauncherFactory.create(config); |
| 158 | + launcher.execute(request); |
| 159 | + }); |
| 160 | + } |
| 161 | +} |
0 commit comments