Skip to content

Commit 4ba84e6

Browse files
committed
add flaky and muted
1 parent eb342f5 commit 4ba84e6

18 files changed

Lines changed: 294 additions & 150 deletions

File tree

allure-java-commons/src/main/java/io/qameta/allure/Flaky.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.lang.annotation.Documented;
44
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
56
import java.lang.annotation.Retention;
67
import java.lang.annotation.RetentionPolicy;
78
import java.lang.annotation.Target;
@@ -14,9 +15,8 @@
1415
* Date: 10.24.13
1516
*/
1617
@Documented
18+
@Inherited
1719
@Retention(RetentionPolicy.RUNTIME)
1820
@Target({ElementType.METHOD, ElementType.TYPE})
1921
public @interface Flaky {
20-
21-
String value();
2222
}

allure-java-commons/src/main/java/io/qameta/allure/Muted.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.lang.annotation.Documented;
44
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
56
import java.lang.annotation.Retention;
67
import java.lang.annotation.RetentionPolicy;
78
import java.lang.annotation.Target;
@@ -11,9 +12,8 @@
1112
* Date: 10.24.13
1213
*/
1314
@Documented
15+
@Inherited
1416
@Retention(RetentionPolicy.RUNTIME)
1517
@Target({ElementType.METHOD, ElementType.TYPE})
1618
public @interface Muted {
17-
18-
String value();
1919
}

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.qameta.allure.testng;
22

33
import io.qameta.allure.Allure;
4+
import io.qameta.allure.Flaky;
5+
import io.qameta.allure.Muted;
46
import io.qameta.allure.model.FixtureResult;
57
import io.qameta.allure.model.Label;
68
import io.qameta.allure.model.Parameter;
@@ -12,6 +14,7 @@
1214
import org.slf4j.Logger;
1315
import org.slf4j.LoggerFactory;
1416
import org.testng.IAttributes;
17+
import org.testng.IClass;
1518
import org.testng.IInvokedMethod;
1619
import org.testng.IInvokedMethodListener2;
1720
import org.testng.ISuite;
@@ -21,9 +24,11 @@
2124
import org.testng.ITestListener;
2225
import org.testng.ITestNGMethod;
2326
import org.testng.ITestResult;
27+
import org.testng.internal.ConstructorOrMethod;
2428
import org.testng.xml.XmlSuite;
2529
import org.testng.xml.XmlTest;
2630

31+
import java.lang.annotation.Annotation;
2732
import java.lang.management.ManagementFactory;
2833
import java.math.BigInteger;
2934
import java.net.InetAddress;
@@ -155,6 +160,9 @@ public void onTestStart(ITestResult testResult) {
155160
.withName(testResult.getName())
156161
.withFullName(testResult.getMethod().getQualifiedName())
157162
.withDescription(method.getDescription())
163+
.withStatusDetails(new StatusDetails()
164+
.withFlaky(isFlaky(testResult))
165+
.withMuted(isMuted(testResult)))
158166
.withParameters(getParameters(testResult))
159167
.withLabels(labels);
160168
getLifecycle().scheduleTestCase(parentUuid, result);
@@ -182,7 +190,7 @@ public void onTestFailure(ITestResult result) {
182190

183191
//if test has failed without any setup
184192
if (!current.isStarted()) {
185-
createTestResutForUnexecutedTest(result);
193+
createTestResultForTestWithoutSetup(result);
186194
}
187195

188196
current.after();
@@ -206,7 +214,7 @@ public void onTestSkipped(ITestResult result) {
206214

207215
//if test was skipped without any setup
208216
if (!current.isStarted()) {
209-
createTestResutForUnexecutedTest(result);
217+
createTestResultForTestWithoutSetup(result);
210218
}
211219
current.after();
212220
StatusDetails details = getStatusDetails(result.getThrowable()).orElse(null);
@@ -215,7 +223,7 @@ public void onTestSkipped(ITestResult result) {
215223
getLifecycle().writeTestCase(current.getUuid());
216224
}
217225

218-
private void createTestResutForUnexecutedTest(ITestResult result) {
226+
private void createTestResultForTestWithoutSetup(ITestResult result) {
219227
onTestStart(result);
220228
currentTestResult.remove();
221229
}
@@ -345,6 +353,35 @@ public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
345353
//do nothing
346354
}
347355

356+
private boolean isFlaky(ITestResult result) {
357+
return hasAnnotation(result, Flaky.class);
358+
}
359+
360+
private boolean isMuted(ITestResult result) {
361+
return hasAnnotation(result, Muted.class);
362+
}
363+
364+
private boolean hasAnnotation(ITestResult result, Class<? extends Annotation> clazz) {
365+
return hasAnnotationOnMethod(result, clazz) || hasAnnotationOnClass(result, clazz);
366+
}
367+
368+
private boolean hasAnnotationOnClass(ITestResult result, Class<? extends Annotation> clazz) {
369+
return Optional.of(result)
370+
.map(ITestResult::getTestClass)
371+
.map(IClass::getRealClass)
372+
.map(aClass -> aClass.isAnnotationPresent(clazz))
373+
.orElse(false);
374+
}
375+
376+
private boolean hasAnnotationOnMethod(ITestResult result, Class<? extends Annotation> clazz) {
377+
return Optional.of(result)
378+
.map(ITestResult::getMethod)
379+
.map(ITestNGMethod::getConstructorOrMethod)
380+
.map(ConstructorOrMethod::getMethod)
381+
.map(method -> method.isAnnotationPresent(clazz))
382+
.orElse(false);
383+
}
384+
348385
/**
349386
* Returns the unique id for given results item.
350387
*/

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

Lines changed: 0 additions & 120 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.qameta.allure.samples;
2+
3+
import io.qameta.allure.Flaky;
4+
import org.testng.annotations.Test;
5+
6+
/**
7+
* @author charlie (Dmitry Baev).
8+
*/
9+
public class FlakyMethods {
10+
11+
@Test
12+
@Flaky
13+
public void flakyTest() throws Exception {
14+
}
15+
16+
@Test
17+
public void notFlaky() throws Exception {
18+
}
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.qameta.allure.samples;
2+
3+
/**
4+
* @author charlie (Dmitry Baev).
5+
*/
6+
public class FlakyMethodsInherited extends FlakyMethods {
7+
8+
@Override
9+
public void flakyTest() throws Exception {
10+
super.flakyTest();
11+
}
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.qameta.allure.samples;
2+
3+
import io.qameta.allure.Flaky;
4+
import org.testng.annotations.Test;
5+
6+
/**
7+
* @author charlie (Dmitry Baev).
8+
*/
9+
@Flaky
10+
public class FlakyTestClass {
11+
12+
@Test
13+
@Flaky
14+
public void flakyTest() throws Exception {
15+
}
16+
17+
@Test
18+
public void flakyAsWell() throws Exception {
19+
}
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.qameta.allure.samples;
2+
3+
import org.testng.annotations.Test;
4+
5+
/**
6+
* @author charlie (Dmitry Baev).
7+
*/
8+
public class FlakyTestClassInherited extends FlakyTestClass {
9+
10+
@Override
11+
public void flakyTest() throws Exception {
12+
super.flakyTest();
13+
}
14+
15+
@Override
16+
public void flakyAsWell() throws Exception {
17+
super.flakyAsWell();
18+
}
19+
20+
@Test
21+
public void flakyInherited() throws Exception {
22+
}
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.qameta.allure.samples;
2+
3+
import io.qameta.allure.Muted;
4+
import org.testng.annotations.Test;
5+
6+
/**
7+
* @author charlie (Dmitry Baev).
8+
*/
9+
public class MutedMethods {
10+
11+
@Test
12+
@Muted
13+
public void mutedTest() throws Exception {
14+
}
15+
16+
@Test
17+
public void notMuted() throws Exception {
18+
}
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.qameta.allure.samples;
2+
3+
/**
4+
* @author charlie (Dmitry Baev).
5+
*/
6+
public class MutedMethodsInherited extends MutedMethods {
7+
8+
@Override
9+
public void mutedTest() throws Exception {
10+
super.mutedTest();
11+
}
12+
}

0 commit comments

Comments
 (0)