Skip to content

Commit 2bc9bcb

Browse files
authored
add support for given stories for jbehave (fixes allure-framework#163, via allure-framework#271)
1 parent 0be5fe0 commit 2bc9bcb

6 files changed

Lines changed: 159 additions & 21 deletions

File tree

allure-jbehave/src/main/java/io/qameta/allure/jbehave/AllureJbehave.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.security.MessageDigest;
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
21+
import java.util.Deque;
22+
import java.util.LinkedList;
2123
import java.util.List;
2224
import java.util.Map;
2325
import java.util.UUID;
@@ -52,6 +54,8 @@ public class AllureJbehave extends NullStoryReporter {
5254

5355
private final Map<Scenario, List<String>> scenarioUuids = new ConcurrentHashMap<>();
5456

57+
private final ThreadLocal<Deque<Story>> givenStories = ThreadLocal.withInitial(LinkedList::new);
58+
5559
private final ReadWriteLock lock = new ReentrantReadWriteLock();
5660

5761
@SuppressWarnings("unused")
@@ -65,16 +69,27 @@ public AllureJbehave(final AllureLifecycle lifecycle) {
6569

6670
@Override
6771
public void beforeStory(final Story story, final boolean givenStory) {
68-
currentStory.set(story);
72+
if (givenStory) {
73+
givenStories.get().push(story);
74+
} else {
75+
currentStory.set(story);
76+
}
6977
}
7078

7179
@Override
7280
public void afterStory(final boolean givenStory) {
73-
currentStory.remove();
81+
if (givenStory) {
82+
givenStories.get().pop();
83+
} else {
84+
currentStory.remove();
85+
}
7486
}
7587

7688
@Override
7789
public void beforeScenario(final Scenario scenario) {
90+
if (isGivenStory()) {
91+
return;
92+
}
7893
currentScenario.set(scenario);
7994

8095
if (notParameterised(scenario)) {
@@ -88,6 +103,9 @@ public void beforeScenario(final Scenario scenario) {
88103

89104
@Override
90105
public void beforeExamples(final List<String> steps, final ExamplesTable table) {
106+
if (isGivenStory()) {
107+
return;
108+
}
91109
final Scenario scenario = currentScenario.get();
92110
lock.writeLock().lock();
93111
try {
@@ -99,6 +117,9 @@ public void beforeExamples(final List<String> steps, final ExamplesTable table)
99117

100118
@Override
101119
public void example(final Map<String, String> tableRow) {
120+
if (isGivenStory()) {
121+
return;
122+
}
102123
final Scenario scenario = currentScenario.get();
103124
final String uuid = UUID.randomUUID().toString();
104125
usingWriteLock(() -> scenarioUuids.getOrDefault(scenario, new ArrayList<>()).add(uuid));
@@ -107,6 +128,9 @@ public void example(final Map<String, String> tableRow) {
107128

108129
@Override
109130
public void afterScenario() {
131+
if (isGivenStory()) {
132+
return;
133+
}
110134
final Scenario scenario = currentScenario.get();
111135
usingReadLock(() -> {
112136
final List<String> uuids = scenarioUuids.getOrDefault(scenario, emptyList());
@@ -232,6 +256,10 @@ protected String getHistoryId(final String fullName, final List<Parameter> param
232256
return bytesToHex(bytes);
233257
}
234258

259+
private boolean isGivenStory() {
260+
return !givenStories.get().isEmpty();
261+
}
262+
235263
private void usingReadLock(final Runnable runnable) {
236264
lock.readLock().lock();
237265
try {

allure-jbehave/src/test/java/io/qameta/allure/jbehave/AllureJbehaveTest.java

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import io.qameta.allure.Issue;
77
import io.qameta.allure.jbehave.samples.BrokenStorySteps;
88
import io.qameta.allure.jbehave.samples.SimpleStorySteps;
9-
import io.qameta.allure.model.ExecutableItem;
109
import io.qameta.allure.model.Parameter;
1110
import io.qameta.allure.model.Stage;
1211
import io.qameta.allure.model.Status;
@@ -31,9 +30,9 @@
3130
import java.io.File;
3231
import java.nio.file.Path;
3332
import java.time.Instant;
33+
import java.util.Arrays;
3434
import java.util.List;
3535

36-
import static java.util.Collections.singletonList;
3736
import static org.assertj.core.api.Assertions.assertThat;
3837
import static org.assertj.core.api.Assertions.tuple;
3938

@@ -52,7 +51,7 @@ public AllureJbehaveTest(@TempDirectory.TempDir final Path temp) {
5251

5352
@Test
5453
void shouldSetName() {
55-
final AllureResults results = runStory("stories/simple.story");
54+
final AllureResults results = runStories("stories/simple.story");
5655

5756
assertThat(results.getTestResults())
5857
.extracting(TestResult::getName)
@@ -63,7 +62,7 @@ void shouldSetName() {
6362
@SuppressWarnings("unchecked")
6463
@Test
6564
void shouldAddNotPerformedSteps() {
66-
final AllureResults results = runStory("stories/long.story");
65+
final AllureResults results = runStories("stories/long.story");
6766

6867
assertThat(results.getTestResults())
6968
.flatExtracting(TestResult::getSteps)
@@ -87,7 +86,7 @@ void shouldAddNotPerformedSteps() {
8786

8887
@Test
8988
void shouldSetStatus() {
90-
final AllureResults results = runStory("stories/simple.story");
89+
final AllureResults results = runStories("stories/simple.story");
9190

9291
assertThat(results.getTestResults())
9392
.extracting(TestResult::getStatus)
@@ -97,7 +96,7 @@ void shouldSetStatus() {
9796

9897
@Test
9998
void shouldSetFailedStatus() {
100-
final AllureResults results = runStory("stories/failed.story");
99+
final AllureResults results = runStories("stories/failed.story");
101100

102101
final List<TestResult> testResults = results.getTestResults();
103102
assertThat(testResults)
@@ -107,7 +106,7 @@ void shouldSetFailedStatus() {
107106

108107
@Test
109108
void shouldSetStatusDetails() {
110-
final AllureResults results = runStory("stories/failed.story");
109+
final AllureResults results = runStories("stories/failed.story");
111110

112111
assertThat(results.getTestResults())
113112
.extracting(TestResult::getStatusDetails)
@@ -117,7 +116,7 @@ void shouldSetStatusDetails() {
117116

118117
@Test
119118
void shouldSetBrokenStatus() {
120-
final AllureResults results = runStory("stories/broken.story");
119+
final AllureResults results = runStories("stories/broken.story");
121120

122121
assertThat(results.getTestResults())
123122
.extracting(TestResult::getStatus)
@@ -126,7 +125,7 @@ void shouldSetBrokenStatus() {
126125

127126
@Test
128127
void shouldSetStage() {
129-
final AllureResults results = runStory("stories/simple.story");
128+
final AllureResults results = runStories("stories/simple.story");
130129

131130
assertThat(results.getTestResults())
132131
.extracting(TestResult::getStage)
@@ -136,7 +135,7 @@ void shouldSetStage() {
136135
@Test
137136
void shouldSetStart() {
138137
final long before = Instant.now().toEpochMilli();
139-
final AllureResults results = runStory("stories/simple.story");
138+
final AllureResults results = runStories("stories/simple.story");
140139
final long after = Instant.now().toEpochMilli();
141140

142141
assertThat(results.getTestResults())
@@ -147,7 +146,7 @@ void shouldSetStart() {
147146
@Test
148147
void shouldSetStop() {
149148
final long before = Instant.now().toEpochMilli();
150-
final AllureResults results = runStory("stories/simple.story");
149+
final AllureResults results = runStories("stories/simple.story");
151150
final long after = Instant.now().toEpochMilli();
152151

153152
assertThat(results.getTestResults())
@@ -157,7 +156,7 @@ void shouldSetStop() {
157156

158157
@Test
159158
void shouldSetFullName() {
160-
final AllureResults results = runStory("stories/simple.story");
159+
final AllureResults results = runStories("stories/simple.story");
161160

162161
assertThat(results.getTestResults())
163162
.extracting(TestResult::getFullName)
@@ -166,7 +165,7 @@ void shouldSetFullName() {
166165

167166
@Test
168167
void shouldSetDescription() {
169-
final AllureResults results = runStory("stories/description.story");
168+
final AllureResults results = runStories("stories/description.story");
170169

171170
final String expected = "This is description for current story.\n"
172171
+ "It should appear on each scenario in report";
@@ -182,7 +181,7 @@ void shouldSetDescription() {
182181
@Issue("238")
183182
@Test
184183
void shouldNotFailOnComments() {
185-
final AllureResults results = runStory("stories/comment.story");
184+
final AllureResults results = runStories("stories/comment.story");
186185

187186
assertThat(results.getTestResults())
188187
.extracting(TestResult::getName, TestResult::getStatus)
@@ -194,7 +193,7 @@ void shouldNotFailOnComments() {
194193

195194
@Test
196195
void shouldProcessNotImplementedScenario() {
197-
final AllureResults results = runStory("stories/undefined.story");
196+
final AllureResults results = runStories("stories/undefined.story");
198197

199198
assertThat(results.getTestResults())
200199
.extracting(TestResult::getName, TestResult::getStatus)
@@ -207,7 +206,7 @@ void shouldProcessNotImplementedScenario() {
207206
@SuppressWarnings("unchecked")
208207
@Test
209208
void shouldAddParametersFromExamples() {
210-
final AllureResults results = runStory("stories/examples.story");
209+
final AllureResults results = runStories("stories/examples.story");
211210

212211
final List<TestResult> testResults = results.getTestResults();
213212

@@ -226,7 +225,7 @@ void shouldAddParametersFromExamples() {
226225

227226
@Test
228227
void shouldRunMultiplyScenarios() {
229-
final AllureResults results = runStory("stories/multiply.story");
228+
final AllureResults results = runStories("stories/multiply.story");
230229

231230
assertThat(results.getTestResults())
232231
.extracting(TestResult::getName, TestResult::getStatus)
@@ -238,7 +237,28 @@ void shouldRunMultiplyScenarios() {
238237

239238
}
240239

241-
private AllureResults runStory(final String storyResource) {
240+
@Issue("163")
241+
@Test
242+
void shouldNotFailIfGivenStoriesSpecified() {
243+
final AllureResults results = runStories("stories/given.story");
244+
245+
assertThat(results.getTestResults())
246+
.extracting(TestResult::getName, TestResult::getStatus)
247+
.containsExactly(tuple("Add a to b", Status.PASSED));
248+
249+
assertThat(results.getTestResults())
250+
.flatExtracting(TestResult::getSteps)
251+
.extracting(StepResult::getName)
252+
.containsExactly(
253+
"Given a is 5",
254+
"Given b is 10",
255+
"When I add a to b",
256+
"Then result is 15"
257+
);
258+
259+
}
260+
261+
private AllureResults runStories(final String... storyResources) {
242262
final AllureResultsWriterStub writer = new AllureResultsWriterStub();
243263
final AllureLifecycle lifecycle = new AllureLifecycle(writer);
244264

@@ -270,7 +290,7 @@ private AllureResults runStory(final String storyResource) {
270290
final AllureLifecycle cached = Allure.getLifecycle();
271291
try {
272292
Allure.setLifecycle(lifecycle);
273-
embedder.runStoriesAsPaths(singletonList(storyResource));
293+
embedder.runStoriesAsPaths(Arrays.asList(storyResources));
274294
} finally {
275295
Allure.setLifecycle(cached);
276296
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package io.qameta.allure.jbehave;
2+
3+
import org.jbehave.core.model.GivenStories;
4+
import org.jbehave.core.model.Scenario;
5+
import org.jbehave.core.model.Story;
6+
import org.jbehave.core.reporters.NullStoryReporter;
7+
8+
import java.util.Map;
9+
import java.util.Objects;
10+
import java.util.concurrent.atomic.AtomicInteger;
11+
import java.util.stream.Collectors;
12+
import java.util.stream.IntStream;
13+
import java.util.stream.Stream;
14+
15+
/**
16+
* @author charlie (Dmitry Baev).
17+
*/
18+
@SuppressWarnings("unused")
19+
class LoggingReporter extends NullStoryReporter {
20+
21+
private final AtomicInteger indentSize = new AtomicInteger(0);
22+
23+
@Override
24+
public void beforeStory(final Story story, final boolean givenStory) {
25+
log("beforeStory", story.getName(), givenStory);
26+
}
27+
28+
@Override
29+
public void beforeGivenStories() {
30+
log("beforeGivenStories");
31+
}
32+
33+
@Override
34+
public void givenStories(final GivenStories givenStories) {
35+
log("givenStories", givenStories.asString());
36+
}
37+
38+
@Override
39+
public void afterGivenStories() {
40+
log("afterGivenStories");
41+
}
42+
43+
@Override
44+
public void beforeScenario(final Scenario scenario) {
45+
log("beforeScenario", scenario.getTitle());
46+
indentSize.incrementAndGet();
47+
}
48+
49+
@Override
50+
public void afterScenario() {
51+
log("afterScenario");
52+
indentSize.decrementAndGet();
53+
}
54+
55+
@Override
56+
public void beforeStep(final String step) {
57+
log("step", step);
58+
}
59+
60+
@Override
61+
public void example(final Map<String, String> tableRow) {
62+
log("example", tableRow);
63+
}
64+
65+
private void log(final Object... objects) {
66+
final int value = indentSize.get();
67+
final String string = Stream.concat(
68+
Stream.of(indent(value)),
69+
Stream.of(objects).map(Objects::toString)
70+
).collect(Collectors.joining(" "));
71+
System.out.println(string);
72+
}
73+
74+
private static String indent(final int count) {
75+
return IntStream.range(0, count)
76+
.mapToObj(i -> " ")
77+
.collect(Collectors.joining());
78+
}
79+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Scenario: Add a to b
2+
GivenStories:stories/precondition-a.story,stories/precondition-b.story
3+
4+
When I add a to b
5+
Then result is 15
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Scenario: Init a
2+
3+
Given a is 5
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Scenario: Init b
2+
3+
Given b is 10

0 commit comments

Comments
 (0)