Skip to content

Commit 6531def

Browse files
author
Vitaly Bragin
authored
add support for meta annotations for spock (fixes #424, via #425)
1 parent 040bb0e commit 6531def

File tree

9 files changed

+236
-51
lines changed

9 files changed

+236
-51
lines changed

allure-spock/src/main/java/io/qameta/allure/spock/AllureSpock.java

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,14 @@
1717

1818
import io.qameta.allure.Allure;
1919
import io.qameta.allure.AllureLifecycle;
20-
import io.qameta.allure.Epic;
21-
import io.qameta.allure.Feature;
2220
import io.qameta.allure.Flaky;
23-
import io.qameta.allure.Issue;
24-
import io.qameta.allure.Link;
2521
import io.qameta.allure.Muted;
26-
import io.qameta.allure.Owner;
27-
import io.qameta.allure.Severity;
28-
import io.qameta.allure.Story;
29-
import io.qameta.allure.TmsLink;
3022
import io.qameta.allure.model.Label;
3123
import io.qameta.allure.model.Parameter;
3224
import io.qameta.allure.model.Status;
3325
import io.qameta.allure.model.StatusDetails;
3426
import io.qameta.allure.model.TestResult;
35-
import io.qameta.allure.util.ResultsUtils;
27+
import io.qameta.allure.util.AnnotationUtils;
3628
import org.junit.runner.Description;
3729
import org.spockframework.runtime.AbstractRunListener;
3830
import org.spockframework.runtime.extension.IGlobalExtension;
@@ -52,8 +44,8 @@
5244
import java.util.Collections;
5345
import java.util.List;
5446
import java.util.Objects;
47+
import java.util.Set;
5548
import java.util.UUID;
56-
import java.util.function.Function;
5749
import java.util.stream.Collectors;
5850
import java.util.stream.IntStream;
5951
import java.util.stream.Stream;
@@ -70,6 +62,7 @@
7062
import static io.qameta.allure.util.ResultsUtils.createTestMethodLabel;
7163
import static io.qameta.allure.util.ResultsUtils.createThreadLabel;
7264
import static io.qameta.allure.util.ResultsUtils.firstNonEmpty;
65+
import static io.qameta.allure.util.ResultsUtils.getProvidedLabels;
7366
import static io.qameta.allure.util.ResultsUtils.getStatus;
7467
import static io.qameta.allure.util.ResultsUtils.getStatusDetails;
7568
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -147,6 +140,7 @@ public void beforeIteration(final IterationInfo iteration) {
147140
labels.add(createParentSuiteLabel(superSpec.getName()));
148141
}
149142
labels.addAll(getLabels(iteration));
143+
labels.addAll(getProvidedLabels());
150144

151145
final TestResult result = new TestResult()
152146
.setUuid(uuid)
@@ -168,25 +162,13 @@ public void beforeIteration(final IterationInfo iteration) {
168162
}
169163

170164
private List<Label> getLabels(final IterationInfo iterationInfo) {
171-
return Stream.of(
172-
getLabels(iterationInfo, Epic.class, ResultsUtils::createLabel),
173-
getLabels(iterationInfo, Feature.class, ResultsUtils::createLabel),
174-
getLabels(iterationInfo, Story.class, ResultsUtils::createLabel),
175-
getLabels(iterationInfo, Severity.class, ResultsUtils::createLabel),
176-
getLabels(iterationInfo, Owner.class, ResultsUtils::createLabel)
177-
).reduce(Stream::concat).orElseGet(Stream::empty).collect(Collectors.toList());
178-
}
179-
180-
private <T extends Annotation> Stream<Label> getLabels(final IterationInfo iterationInfo, final Class<T> clazz,
181-
final Function<T, Label> extractor) {
182-
final List<Label> onFeature = getFeatureAnnotations(iterationInfo, clazz).stream()
183-
.map(extractor)
184-
.collect(Collectors.toList());
185-
if (!onFeature.isEmpty()) {
186-
return onFeature.stream();
187-
}
188-
return getSpecAnnotations(iterationInfo, clazz).stream()
189-
.map(extractor);
165+
final Set<Label> labels = AnnotationUtils.getLabels(
166+
iterationInfo.getFeature().getDescription().getAnnotations()
167+
);
168+
labels.addAll(AnnotationUtils.getLabels(
169+
iterationInfo.getFeature().getSpec().getDescription().getAnnotations()
170+
));
171+
return new ArrayList<>(labels);
190172
}
191173

192174
private void processDescription(final IterationInfo iterationInfo, final TestResult item) {
@@ -243,14 +225,10 @@ private boolean hasAnnotationOnFeature(final IterationInfo iteration, final Clas
243225
}
244226

245227
private List<io.qameta.allure.model.Link> getLinks(final IterationInfo iteration) {
246-
return Stream.of(
247-
getSpecAnnotations(iteration, Link.class).stream().map(ResultsUtils::createLink),
248-
getFeatureAnnotations(iteration, Link.class).stream().map(ResultsUtils::createLink),
249-
getSpecAnnotations(iteration, Issue.class).stream().map(ResultsUtils::createLink),
250-
getFeatureAnnotations(iteration, Issue.class).stream().map(ResultsUtils::createLink),
251-
getSpecAnnotations(iteration, TmsLink.class).stream().map(ResultsUtils::createLink),
252-
getFeatureAnnotations(iteration, TmsLink.class).stream().map(ResultsUtils::createLink)
253-
).reduce(Stream::concat).orElseGet(Stream::empty).collect(Collectors.toList());
228+
final List<io.qameta.allure.model.Link> links = new ArrayList<>();
229+
links.addAll(AnnotationUtils.getLinks(iteration.getFeature().getDescription().getAnnotations()));
230+
links.addAll(AnnotationUtils.getLinks(iteration.getFeature().getSpec().getDescription().getAnnotations()));
231+
return links;
254232
}
255233

256234
private <T extends Annotation> List<T> getAnnotationsOnMethod(final Description result, final Class<T> clazz) {

allure-spock/src/test/groovy/io/qameta/allure/spock/AllureSpockTest.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import io.qameta.allure.spock.samples.OneTest;
3434
import io.qameta.allure.spock.samples.ParametersTest;
3535
import io.qameta.allure.spock.samples.TestWithAnnotations;
36+
import io.qameta.allure.spock.samples.TestWithAnnotationsOnClass;
37+
import io.qameta.allure.spock.samples.TestWithCustomAnnotations;
3638
import io.qameta.allure.spock.samples.TestWithSteps;
3739
import io.qameta.allure.test.AllureResults;
3840
import io.qameta.allure.test.AllureResultsWriterStub;
@@ -142,6 +144,33 @@ void shouldProcessMethodAnnotations() {
142144
);
143145
}
144146

147+
@Test
148+
void shouldProcessClassAnnotations() {
149+
final AllureResults results = run(TestWithAnnotationsOnClass.class);
150+
assertThat(results.getTestResults())
151+
.hasSize(1)
152+
.flatExtracting(TestResult::getLabels)
153+
.extracting(Label::getValue)
154+
.contains(
155+
"epic1", "epic2", "epic3",
156+
"feature1", "feature2", "feature3",
157+
"story1", "story2", "story3",
158+
"some-owner"
159+
);
160+
}
161+
162+
@Test
163+
void shouldProcessCustomAnnotations() {
164+
final AllureResults results = run(TestWithCustomAnnotations.class);
165+
assertThat(results.getTestResults())
166+
.hasSize(1)
167+
.flatExtracting(TestResult::getLabels)
168+
.extracting(Label::getValue)
169+
.contains(
170+
"epic", "feature", "story", "AS-1", "XRT-1"
171+
);
172+
}
173+
145174
@Test
146175
void shouldProcessFlakyAnnotation() {
147176
final AllureResults results = run(TestWithAnnotations.class);
@@ -172,7 +201,7 @@ void shouldSetLinks() {
172201
assertThat(results.getTestResults())
173202
.flatExtracting(TestResult::getLinks)
174203
.extracting(Link::getName)
175-
.containsExactly("link-1", "link-2", "issue-1", "issue-2", "tms-1", "tms-2");
204+
.containsExactlyInAnyOrder("link-1", "link-2", "issue-1", "issue-2", "tms-1", "tms-2");
176205
}
177206

178207
@Test
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2020 Qameta Software OÜ
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.spock.samples
17+
18+
import io.qameta.allure.LabelAnnotation
19+
20+
import java.lang.annotation.Documented
21+
import java.lang.annotation.ElementType
22+
import java.lang.annotation.Inherited
23+
import java.lang.annotation.Retention
24+
import java.lang.annotation.RetentionPolicy
25+
import java.lang.annotation.Target
26+
27+
/**
28+
* @author vbragin
29+
*/
30+
@Documented
31+
@Inherited
32+
@Retention(RetentionPolicy.RUNTIME)
33+
@Target(ElementType.METHOD)
34+
@LabelAnnotation(name = "jira")
35+
@interface JiraIssue {
36+
String value();
37+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2019 Qameta Software OÜ
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.spock.samples
17+
18+
import io.qameta.allure.Epic
19+
import io.qameta.allure.Epics
20+
import io.qameta.allure.Feature
21+
import io.qameta.allure.Features
22+
import io.qameta.allure.Flaky
23+
import io.qameta.allure.Muted
24+
import io.qameta.allure.Owner
25+
import io.qameta.allure.Stories
26+
import io.qameta.allure.Story
27+
import org.junit.Test
28+
import spock.lang.Specification
29+
30+
/**
31+
* @author vbragin
32+
*/
33+
@Epic("epic1")
34+
@Features([
35+
@Feature("feature1"),
36+
@Feature("feature2")
37+
])
38+
@Feature("feature3")
39+
@Story("story1")
40+
@Stories([
41+
@Story("story2"),
42+
@Story("story3")]
43+
)
44+
class TestWithAnnotationsOnClass extends Specification {
45+
46+
@Test
47+
@Flaky
48+
@Epics([
49+
@Epic("epic2"),
50+
@Epic("epic3")
51+
])
52+
@Muted
53+
@Owner("some-owner")
54+
def "someTest"() {
55+
expect:
56+
true
57+
}
58+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2019 Qameta Software OÜ
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.spock.samples
17+
18+
import io.qameta.allure.Epic
19+
import io.qameta.allure.Feature
20+
import io.qameta.allure.Story
21+
import org.junit.Test
22+
import spock.lang.Specification
23+
24+
/**
25+
* @author vbragin
26+
*/
27+
class TestWithCustomAnnotations extends Specification {
28+
29+
@Test
30+
@Epic("epic")
31+
@Feature("feature")
32+
@Story("story")
33+
@JiraIssue("AS-1")
34+
@XrayId("XRT-1")
35+
def "someTest"() {
36+
expect:
37+
true
38+
}
39+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2020 Qameta Software OÜ
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.spock.samples
17+
18+
import io.qameta.allure.LabelAnnotation
19+
20+
import java.lang.annotation.Documented
21+
import java.lang.annotation.ElementType
22+
import java.lang.annotation.Inherited
23+
import java.lang.annotation.Retention
24+
import java.lang.annotation.RetentionPolicy
25+
import java.lang.annotation.Target
26+
27+
/**
28+
* @author vbragin
29+
*/
30+
@Documented
31+
@Inherited
32+
@Retention(RetentionPolicy.RUNTIME)
33+
@Target(ElementType.METHOD)
34+
@LabelAnnotation(name = "xray")
35+
@interface XrayId {
36+
String value();
37+
}

build.gradle.kts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ buildscript {
1313
}
1414

1515
dependencies {
16-
classpath("com.diffplug.spotless:spotless-plugin-gradle:3.17.0")
16+
classpath("com.diffplug.spotless:spotless-plugin-gradle:3.27.0")
1717
classpath("com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4")
18-
classpath("gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:1.6.9")
1918
classpath("io.spring.gradle:dependency-management-plugin:1.0.6.RELEASE")
20-
classpath("ru.vyarus:gradle-quality-plugin:3.3.0")
19+
classpath("ru.vyarus:gradle-quality-plugin:4.0.0")
2120
}
2221
}
2322

@@ -32,7 +31,7 @@ val qualityConfigsDir by extra("$gradleScriptDir/quality-configs")
3231
val spotlessDtr by extra("$qualityConfigsDir/spotless")
3332

3433
tasks.withType(Wrapper::class) {
35-
gradleVersion = "5.6.1"
34+
gradleVersion = "6.0.1"
3635
}
3736

3837
plugins {
@@ -102,8 +101,11 @@ configure(subprojects) {
102101
dependency("org.jooq:joor-java-8:0.9.10")
103102
dependency("org.mock-server:mockserver-netty:5.5.1")
104103
dependency("org.mockito:mockito-core:2.24.0")
105-
dependency("org.slf4j:slf4j-api:1.7.25")
106-
dependency("org.slf4j:slf4j-simple:1.7.25")
104+
dependencySet("org.slf4j:1.7.30") {
105+
entry("slf4j-api")
106+
entry("slf4j-nop")
107+
entry("slf4j-simple")
108+
}
107109
}
108110
}
109111

@@ -144,9 +146,9 @@ configure(subprojects) {
144146
configure<QualityExtension> {
145147
configDir = qualityConfigsDir
146148
checkstyleVersion = "8.22"
147-
pmdVersion = "6.16.0"
148-
spotbugsVersion = "3.1.11"
149-
codenarcVersion = "1.3"
149+
pmdVersion = "6.17.0"
150+
spotbugsVersion = "3.1.12"
151+
codenarcVersion = "1.4"
150152
enabled = !project.hasProperty("disableQuality")
151153
}
152154

gradle/quality-configs/spotbugs/exclude.xml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@
2626

2727

2828
<!-- Do not check class -->
29-
<!--<Match>-->
30-
<!--<Class name="com.foobar.ClassNotToBeAnalyzed" />-->
31-
<!--</Match>-->
29+
<Match>
30+
<Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"/>
31+
<And>
32+
<Class name="io.qameta.allure.util.PropertiesUtils" />
33+
<Method name="loadPropertiesFrom"/>
34+
</And>
35+
</Match>
36+
3237
</FindBugsFilter>

0 commit comments

Comments
 (0)