Skip to content

Commit c67f830

Browse files
sspotaninbaev
authored andcommitted
fix steps and attachments for tests with timeouts (fixes allure-framework#158, fixes allure-framework#108, via allure-framework#193)
1 parent 46aee02 commit c67f830

11 files changed

Lines changed: 145 additions & 19 deletions

File tree

allure-java-commons/src/main/java/io/qameta/allure/internal/AllureStorage.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ public class AllureStorage {
2121

2222
private final Map<String, Object> storage = new ConcurrentHashMap<>();
2323

24-
private final ThreadLocal<LinkedList<String>> currentStepContext =
25-
InheritableThreadLocal.withInitial(LinkedList::new);
24+
@SuppressWarnings("checkstyle:LineLength")
25+
private final ThreadLocal<LinkedList<String>> currentStepContext = new InheritableThreadLocal<LinkedList<String>>() {
26+
@Override
27+
public LinkedList<String> initialValue() {
28+
return new LinkedList<>();
29+
}
30+
};
2631

2732
@SuppressWarnings("PMD.NullAssignment")
2833
public Optional<String> getCurrentStep() {

allure-java-commons/src/main/java/io/qameta/allure/util/ResultsUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,3 @@ public static void processDescription(final ClassLoader classLoader, final Metho
276276
}
277277

278278
}
279-

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@
4545
* Allure Junit4 listener.
4646
*/
4747
@RunListener.ThreadSafe
48-
@SuppressWarnings({"PMD.ExcessiveImports", "PMD.CouplingBetweenObjects"})
48+
@SuppressWarnings({"PMD.ExcessiveImports", "PMD.CouplingBetweenObjects", "checkstyle:ClassFanOutComplexity"})
4949
public class AllureJunit4 extends RunListener {
5050

5151
public static final String MD_5 = "md5";
5252

53-
private final ThreadLocal<String> testCases
54-
= InheritableThreadLocal.withInitial(() -> UUID.randomUUID().toString());
53+
private final ThreadLocal<String> testCases = new InheritableThreadLocal<String>() {
54+
@Override
55+
protected String initialValue() {
56+
return UUID.randomUUID().toString();
57+
}
58+
};
5559

5660
private final AllureLifecycle lifecycle;
5761

@@ -279,3 +283,4 @@ private TestResult createTestResult(final String uuid, final Description descrip
279283
}
280284

281285
}
286+

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.qameta.allure.junit4.samples.TaggedTests;
1212
import io.qameta.allure.junit4.samples.TestWithAnnotations;
1313
import io.qameta.allure.junit4.samples.TestWithSteps;
14+
import io.qameta.allure.junit4.samples.TestWithTimeout;
1415
import io.qameta.allure.model.Label;
1516
import io.qameta.allure.model.Link;
1617
import io.qameta.allure.model.Stage;
@@ -172,6 +173,19 @@ public void shouldAddStepsToTest() throws Exception {
172173
.containsExactly("step1", "step2", "step3");
173174
}
174175

176+
@Test
177+
@DisplayName("Test with timeout and steps")
178+
public void testWithTimeoutAndSteps() {
179+
core.run(Request.aClass(TestWithTimeout.class));
180+
List<TestResult> testResults = results.getTestResults();
181+
assertThat(testResults)
182+
.hasSize(1)
183+
.flatExtracting(TestResult::getSteps)
184+
.hasSize(2)
185+
.extracting(StepResult::getName)
186+
.containsExactly("Step 1", "Step 2");
187+
}
188+
175189
@Test
176190
@DisplayName("Annotations on method")
177191
public void shouldProcessMethodAnnotations() throws Exception {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.qameta.allure.junit4.samples;
2+
3+
import io.qameta.allure.Step;
4+
import org.junit.Test;
5+
6+
/**
7+
* Author: Sergey Potanin
8+
* Date: 02/02/2018
9+
*/
10+
public class TestWithTimeout {
11+
12+
@Test(timeout = 100)
13+
public void testWithSteps() {
14+
step1();
15+
step2();
16+
}
17+
18+
@Step("Step 1")
19+
private void step1() {
20+
}
21+
22+
@Step("Step 2")
23+
private void step2() {
24+
}
25+
26+
}

allure-testng/src/main/java/io/qameta/allure/testng/AllureTestNg.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,32 @@ public class AllureTestNg implements ISuiteListener, ITestListener, IInvokedMeth
7777
/**
7878
* Store current testng result uuid to attach before/after methods into.
7979
*/
80-
private final ThreadLocal<Current> currentTestResult
81-
= InheritableThreadLocal.withInitial(Current::new);
80+
private final ThreadLocal<Current> currentTestResult = new InheritableThreadLocal<Current>() {
81+
@Override
82+
protected Current initialValue() {
83+
return new Current();
84+
}
85+
};
8286

8387
/**
8488
* Store current container uuid for fake containers around before/after methods.
8589
*/
86-
private final ThreadLocal<String> currentTestContainer
87-
= InheritableThreadLocal.withInitial(() -> UUID.randomUUID().toString());
90+
private final ThreadLocal<String> currentTestContainer = new InheritableThreadLocal<String>() {
91+
@Override
92+
protected String initialValue() {
93+
return UUID.randomUUID().toString();
94+
}
95+
};
8896

8997
/**
9098
* Store uuid for current executable item to catch steps and attachments.
9199
*/
92-
private final ThreadLocal<String> currentExecutable
93-
= InheritableThreadLocal.withInitial(() -> UUID.randomUUID().toString());
100+
private final ThreadLocal<String> currentExecutable = new InheritableThreadLocal<String>() {
101+
@Override
102+
protected String initialValue() {
103+
return UUID.randomUUID().toString();
104+
}
105+
};
94106

95107
/**
96108
* Store uuid for class test containers.

allure-testng/src/test/java/io/qameta/allure/testng/AllureTestNgTest.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.stream.Collectors;
3737

3838
import static java.lang.String.format;
39+
import static java.util.Arrays.asList;
3940
import static java.util.Collections.singletonList;
4041
import static org.assertj.core.api.Assertions.assertThat;
4142
import static org.assertj.core.api.Assertions.tuple;
@@ -97,6 +98,35 @@ public void singleTest() {
9798
.contains(Status.PASSED);
9899
}
99100

101+
@Feature("Basic framework support")
102+
@Test(description = "Test with timeout")
103+
public void testWithTimeout() {
104+
final String testNameWithTimeout = "testWithTimeout";
105+
final String testNameWithoutTimeout = "testWithoutTimeout";
106+
runTestNgSuites("suites/tests-with-timeout.xml");
107+
List<TestResult> testResults = results.getTestResults();
108+
109+
assertThat(testResults)
110+
.as("Test case results have not been written")
111+
.hasSize(2)
112+
.as("Unexpectedly passed status or stage of tests")
113+
.allMatch(testResult -> testResult.getStatus().equals(Status.PASSED) &&
114+
testResult.getStage().equals(Stage.FINISHED))
115+
.extracting(TestResult::getName)
116+
.as("Unexpectedly passed name of tests")
117+
.containsOnlyElementsOf(asList(
118+
testNameWithoutTimeout,
119+
testNameWithTimeout));
120+
assertThat(testResults)
121+
.flatExtracting(TestResult::getSteps)
122+
.as("No steps present for test with timeout")
123+
.hasSize(2)
124+
.extracting(StepResult::getName)
125+
.containsOnlyElementsOf(asList(
126+
"Step of the test with timeout",
127+
"Step of the test with no timeout"));
128+
}
129+
100130
@Feature("Descriptions")
101131
@Test(description = "Javadoc descriptions of tests")
102132
public void descriptionsTest() {
@@ -461,7 +491,7 @@ public void linksTest() throws Exception {
461491
public void bddAnnotationsTest() throws Exception {
462492
runTestNgSuites("suites/bdd-annotations.xml");
463493

464-
List<String> bddLabels = Arrays.asList("epic", "feature", "story");
494+
List<String> bddLabels = asList("epic", "feature", "story");
465495
List<TestResult> testResults = results.getTestResults();
466496
assertThat(testResults)
467497
.hasSize(2)

allure-testng/src/test/java/io/qameta/allure/testng/samples/DescriptionsTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010
public class DescriptionsTest {
1111

12-
1312
/**
1413
* Sample test description
1514
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.qameta.allure.testng.samples;
2+
3+
import io.qameta.allure.Step;
4+
import org.testng.annotations.Test;
5+
6+
/**
7+
* Author: Sergey Potanin
8+
* Date: 31/01/2018
9+
*/
10+
public class TestWithTimeout {
11+
12+
@Test(timeOut = 100)
13+
public void testWithTimeout() {
14+
stepForTimeout();
15+
}
16+
17+
@Test(timeOut = 0)
18+
public void testWithoutTimeout() {
19+
step();
20+
}
21+
22+
@Step("Step of the test with timeout")
23+
private void stepForTimeout() {
24+
}
25+
26+
@Step("Step of the test with no timeout")
27+
private void step() {
28+
}
29+
30+
}

allure-testng/src/test/resources/suites/descriptions-test.xml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
<suite name="Descriptions suite">
55
<test name="Descriptions test">
66
<classes>
7-
<class name="io.qameta.allure.testng.samples.DescriptionsTest" >
8-
<methods>
9-
<include name="test"/>
10-
</methods>
11-
</class>
7+
<class name="io.qameta.allure.testng.samples.DescriptionsTest"/>
128
</classes>
139
</test>
1410
</suite>

0 commit comments

Comments
 (0)