Skip to content

Commit eae5d23

Browse files
gladnikbaev
authored andcommitted
process skipped and ignored tests for junit4 (fixes allure-framework#112, via allure-framework#109)
1 parent dadb8a2 commit eae5d23

4 files changed

Lines changed: 130 additions & 26 deletions

File tree

allure-junit4/src/main/java/io/qameta/allure/junit4/AllureJunit4.java

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
import io.qameta.allure.Epic;
66
import io.qameta.allure.Feature;
77
import io.qameta.allure.Owner;
8-
import io.qameta.allure.util.ResultsUtils;
98
import io.qameta.allure.Severity;
109
import io.qameta.allure.Story;
1110
import io.qameta.allure.model.Label;
1211
import io.qameta.allure.model.Link;
1312
import io.qameta.allure.model.Status;
13+
import io.qameta.allure.model.StatusDetails;
1414
import io.qameta.allure.model.TestResult;
15+
import io.qameta.allure.util.ResultsUtils;
16+
import org.junit.Ignore;
1517
import org.junit.runner.Description;
1618
import org.junit.runner.Result;
1719
import org.junit.runner.notification.Failure;
@@ -43,7 +45,7 @@
4345
* Allure Junit4 listener.
4446
*/
4547
@RunListener.ThreadSafe
46-
@SuppressWarnings("PMD.ExcessiveImports")
48+
@SuppressWarnings({"PMD.ExcessiveImports", "PMD.CouplingBetweenObjects"})
4749
public class AllureJunit4 extends RunListener {
4850

4951
public static final String MD_5 = "md5";
@@ -78,25 +80,7 @@ public void testRunFinished(final Result result) throws Exception {
7880
@Override
7981
public void testStarted(final Description description) throws Exception {
8082
final String uuid = testCases.get();
81-
final String id = getHistoryId(description);
82-
83-
final TestResult result = new TestResult()
84-
.withUuid(uuid)
85-
.withHistoryId(id)
86-
.withName(description.getMethodName())
87-
.withFullName(String.format("%s.%s", description.getClassName(), description.getMethodName()))
88-
.withLinks(getLinks(description))
89-
.withLabels(
90-
new Label().withName("package").withValue(getPackage(description.getTestClass())),
91-
new Label().withName("testClass").withValue(description.getClassName()),
92-
new Label().withName("testMethod").withValue(description.getMethodName()),
93-
94-
new Label().withName("suite").withValue(description.getClassName()),
95-
96-
new Label().withName("host").withValue(getHostName()),
97-
new Label().withName("thread").withValue(getThreadName())
98-
);
99-
83+
final TestResult result = createTestResult(uuid, description);
10084
result.getLabels().addAll(getLabels(description));
10185
getDisplayName(description).ifPresent(result::setName);
10286
getLifecycle().scheduleTestCase(result);
@@ -128,12 +112,28 @@ public void testFailure(final Failure failure) throws Exception {
128112

129113
@Override
130114
public void testAssumptionFailure(final Failure failure) {
131-
//not implemented
115+
final String uuid = testCases.get();
116+
getLifecycle().updateTestCase(uuid, testResult ->
117+
testResult.withStatus(Status.SKIPPED)
118+
.withStatusDetails(getStatusDetails(failure.getException()).orElse(null))
119+
);
132120
}
133121

134122
@Override
135123
public void testIgnored(final Description description) throws Exception {
136-
//not implemented
124+
final String uuid = testCases.get();
125+
testCases.remove();
126+
127+
final TestResult result = createTestResult(uuid, description);
128+
result.getLabels().addAll(getLabels(description));
129+
getDisplayName(description).ifPresent(result::setName);
130+
result.setStatus(Status.SKIPPED);
131+
result.setStatusDetails(getIgnoredMessage(description));
132+
result.setStart(System.currentTimeMillis());
133+
134+
getLifecycle().scheduleTestCase(result);
135+
getLifecycle().stopTestCase(uuid);
136+
getLifecycle().writeTestCase(uuid);
137137
}
138138

139139
private Optional<String> getDisplayName(final Description result) {
@@ -235,4 +235,29 @@ private MessageDigest getMessageDigest() {
235235
private String getPackage(final Class<?> testClass) {
236236
return testClass.getPackage().getName();
237237
}
238+
239+
private StatusDetails getIgnoredMessage(final Description description) {
240+
final Ignore ignore = description.getAnnotation(Ignore.class);
241+
final String message = Objects.nonNull(ignore) && !ignore.value().isEmpty()
242+
? ignore.value() : "Test ignored (without reason)!";
243+
return new StatusDetails().withMessage(message);
244+
}
245+
246+
private TestResult createTestResult(final String uuid, final Description description) {
247+
return new TestResult()
248+
.withUuid(uuid)
249+
.withHistoryId(getHistoryId(description))
250+
.withName(description.getMethodName())
251+
.withFullName(String.format("%s.%s", description.getClassName(), description.getMethodName()))
252+
.withLinks(getLinks(description))
253+
.withLabels(
254+
new Label().withName("package").withValue(getPackage(description.getTestClass())),
255+
new Label().withName("testClass").withValue(description.getClassName()),
256+
new Label().withName("testMethod").withValue(description.getMethodName()),
257+
new Label().withName("suite").withValue(description.getClassName()),
258+
new Label().withName("host").withValue(getHostName()),
259+
new Label().withName("thread").withValue(getThreadName())
260+
);
261+
}
262+
238263
}

allure-junit4/src/test/java/io/qameta/allure/junit4/FeatureCombinationsTest.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,31 @@
22

33
import io.qameta.allure.AllureLifecycle;
44
import io.qameta.allure.aspects.StepsAspects;
5-
import io.qameta.allure.junit4.samples.*;
5+
import io.qameta.allure.junit4.samples.AssumptionFailedTest;
6+
import io.qameta.allure.junit4.samples.BrokenTest;
7+
import io.qameta.allure.junit4.samples.FailedTest;
8+
import io.qameta.allure.junit4.samples.IgnoredTests;
9+
import io.qameta.allure.junit4.samples.OneTest;
10+
import io.qameta.allure.junit4.samples.TaggedTests;
11+
import io.qameta.allure.junit4.samples.TestWithAnnotations;
12+
import io.qameta.allure.junit4.samples.TestWithSteps;
613
import io.qameta.allure.model.Label;
714
import io.qameta.allure.model.Link;
815
import io.qameta.allure.model.Stage;
916
import io.qameta.allure.model.Status;
17+
import io.qameta.allure.model.StatusDetails;
1018
import io.qameta.allure.model.StepResult;
1119
import io.qameta.allure.model.TestResult;
1220
import io.qameta.allure.test.AllureResultsWriterStub;
13-
import org.assertj.core.api.SoftAssertions;
1421
import org.junit.Before;
1522
import org.junit.Test;
1623
import org.junit.runner.JUnitCore;
1724
import org.junit.runner.Request;
1825

1926
import java.util.List;
2027

21-
import static io.qameta.allure.junit4.samples.TaggedTests.*;
28+
import static io.qameta.allure.junit4.samples.TaggedTests.METHOD_TAG1;
29+
import static io.qameta.allure.junit4.samples.TaggedTests.METHOD_TAG2;
2230
import static org.assertj.core.api.Assertions.assertThat;
2331

2432
public class FeatureCombinationsTest {
@@ -102,6 +110,40 @@ public void shouldProcessBrokenTest() throws Exception {
102110
.containsExactly(Status.BROKEN);
103111
}
104112

113+
@Test
114+
@DisplayName("Skipped test")
115+
public void shouldProcessSkippedTest() throws Exception {
116+
core.run(Request.aClass(AssumptionFailedTest.class));
117+
List<TestResult> testResults = results.getTestResults();
118+
assertThat(testResults)
119+
.hasSize(1)
120+
.extracting(TestResult::getStatus)
121+
.containsExactly(Status.SKIPPED);
122+
}
123+
124+
@Test
125+
@DisplayName("Ignored tests")
126+
public void shouldProcessIgnoredTest() throws Exception {
127+
core.run(Request.aClass(IgnoredTests.class));
128+
List<TestResult> testResults = results.getTestResults();
129+
assertThat(testResults)
130+
.hasSize(2)
131+
.flatExtracting(TestResult::getStatus)
132+
.containsExactly(Status.SKIPPED, Status.SKIPPED);
133+
}
134+
135+
@Test
136+
@DisplayName("Ignored tests messages")
137+
public void shouldProcessIgnoredTestDescription() throws Exception {
138+
core.run(Request.aClass(IgnoredTests.class));
139+
List<TestResult> testResults = results.getTestResults();
140+
assertThat(testResults)
141+
.hasSize(2)
142+
.extracting(TestResult::getStatusDetails)
143+
.extracting(StatusDetails::getMessage)
144+
.containsExactlyInAnyOrder("Test ignored (without reason)!", "Ignored for some reason");
145+
}
146+
105147
@Test
106148
@DisplayName("Test with steps")
107149
public void shouldAddStepsToTest() throws Exception {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.qameta.allure.junit4.samples;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.core.Is.is;
6+
import static org.junit.Assume.assumeThat;
7+
8+
/**
9+
* @author gladnik (Nikolai Gladkov)
10+
*/
11+
public class AssumptionFailedTest {
12+
13+
@Test
14+
public void assumptionFailedTest() {
15+
assumeThat(true, is(false));
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.qameta.allure.junit4.samples;
2+
3+
import org.junit.Ignore;
4+
import org.junit.Test;
5+
6+
/**
7+
* @author gladnik (Nikolai Gladkov)
8+
*/
9+
public class IgnoredTests {
10+
11+
@Test
12+
@Ignore
13+
public void ignoredTest() {
14+
}
15+
16+
@Test
17+
@Ignore("Ignored for some reason")
18+
public void ignoredWithDescriptionTest() {
19+
}
20+
}

0 commit comments

Comments
 (0)