Skip to content

Commit fe13d57

Browse files
andrcunsbaev
authored andcommitted
allow embedding attachments and other allure steps in cucumber hook steps (via allure-framework#181)
1 parent 0e325d6 commit fe13d57

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

allure-cucumber2-jvm/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ configurations {
88
agent
99
}
1010

11-
def cucumber_version = '2.1.0'
11+
def cucumber_version = '2.3.1'
1212

1313
dependencies {
1414
agent 'org.aspectj:aspectjweaver'

allure-cucumber2-jvm/src/main/java/io/qameta/allure/cucumber2jvm/AllureCucumber2Jvm.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.util.HashMap;
4343
import java.util.Optional;
4444
import java.util.UUID;
45+
import java.util.function.Consumer;
4546
import java.util.stream.Collectors;
4647
import java.util.stream.IntStream;
4748

@@ -151,8 +152,7 @@ private void handleTestStepStarted(final TestStepStarted event) {
151152
cucumberSourceUtils.getKeywordFromSource(currentFeatureFile, event.testStep.getStepLine())
152153
).orElse("UNDEFINED");
153154

154-
final StepResult stepResult = new StepResult();
155-
stepResult
155+
final StepResult stepResult = new StepResult()
156156
.withName(String.format("%s %s", stepKeyword, event.testStep.getPickleStep().getText()))
157157
.withStart(System.currentTimeMillis());
158158

@@ -162,6 +162,12 @@ private void handleTestStepStarted(final TestStepStarted event) {
162162
.filter(argument -> argument instanceof PickleTable)
163163
.findFirst()
164164
.ifPresent(table -> createDataTableAttachment((PickleTable) table));
165+
} else if (event.testStep.isHook() && event.testStep instanceof UnskipableStep) {
166+
final StepResult stepResult = new StepResult()
167+
.withName(event.testStep.getHookType().toString())
168+
.withStart(System.currentTimeMillis());
169+
170+
lifecycle.startStep(getTestCaseUuid(currentTestCase), getHookStepUuid(event.testStep), stepResult);
165171
}
166172
}
167173

@@ -186,6 +192,11 @@ private String getStepUuid(final TestStep step) {
186192
+ step.getPickleStep().getText() + step.getStepLine();
187193
}
188194

195+
private String getHookStepUuid(final TestStep step) {
196+
return currentFeature.getName() + getTestCaseUuid(currentTestCase)
197+
+ step.getHookType().toString() + step.getCodeLocation();
198+
}
199+
189200
private String getHistoryId(final TestCase testCase) {
190201
final String testCaseLocation = testCase.getUri() + ":" + testCase.getLine();
191202
return Utils.md5(testCaseLocation);
@@ -239,16 +250,11 @@ private void createDataTableAttachment(final PickleTable pickleTable) {
239250
}
240251

241252
private void handleHookStep(final TestStepFinished event) {
242-
final String uuid = UUID.randomUUID().toString();
243-
final StepResult stepResult = new StepResult()
244-
.withName(event.testStep.getHookType().toString())
245-
.withStatus(translateTestCaseStatus(event.result))
246-
.withStart(System.currentTimeMillis() - event.result.getDuration())
247-
.withStop(System.currentTimeMillis());
248-
249-
if (!Status.PASSED.equals(stepResult.getStatus())) {
253+
final String uuid = getHookStepUuid(event.testStep);
254+
Consumer<StepResult> stepResult = result -> result.withStatus(translateTestCaseStatus(event.result));
255+
256+
if (!Status.PASSED.equals(translateTestCaseStatus(event.result))) {
250257
final StatusDetails statusDetails = ResultsUtils.getStatusDetails(event.result.getError()).get();
251-
stepResult.withStatusDetails(statusDetails);
252258
if (event.testStep.getHookType() == HookType.Before) {
253259
final TagParser tagParser = new TagParser(currentFeature, currentTestCase);
254260
statusDetails
@@ -260,9 +266,12 @@ private void handleHookStep(final TestStepFinished event) {
260266
scenarioResult.withStatus(Status.SKIPPED)
261267
.withStatusDetails(statusDetails));
262268
}
269+
stepResult = result -> result
270+
.withStatus(translateTestCaseStatus(event.result))
271+
.withStatusDetails(statusDetails);
263272
}
264273

265-
lifecycle.startStep(getTestCaseUuid(currentTestCase), uuid, stepResult);
274+
lifecycle.updateStep(uuid, stepResult);
266275
lifecycle.stopStep(uuid);
267276
}
268277

0 commit comments

Comments
 (0)