Skip to content

Commit 9009f1e

Browse files
authored
Merge pull request #517 from XiangRongLin/flaky_test
Add MockOnlyRule to allow skipping specific tests based on downloader
2 parents f71cfd4 + e13e237 commit 9009f1e

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.schabi.newpipe;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Inherited;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
import javax.annotation.Nonnull;
10+
11+
/**
12+
* Marker annotation to skip test in certain cases.
13+
*
14+
* {@link MockOnlyRule}
15+
*/
16+
@Retention(RetentionPolicy.RUNTIME)
17+
@Target({ElementType.METHOD, ElementType.TYPE})
18+
@Inherited
19+
public @interface MockOnly {
20+
21+
/**
22+
* Explanation why this test should be skipped
23+
*/
24+
@Nonnull String reason();
25+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.schabi.newpipe;
2+
3+
import org.junit.Assume;
4+
import org.junit.rules.TestRule;
5+
import org.junit.runner.Description;
6+
import org.junit.runners.model.Statement;
7+
import org.schabi.newpipe.downloader.DownloaderType;
8+
9+
import javax.annotation.Nonnull;
10+
11+
/**
12+
*
13+
* <p>
14+
* Allows skipping unreliable or time sensitive tests in CI pipeline.
15+
* </p>
16+
*
17+
* <p>
18+
* Use it by creating a public variable of this inside the test class and annotate it with
19+
* {@link org.junit.Rule}. Then annotate the specific tests to be skipped with {@link MockOnly}
20+
* </p>
21+
*
22+
* <p>
23+
* It works by checking if the system variable "downloader" is set to "REAL" and skips the tests if it is.
24+
* Otherwise it executes the test.
25+
* </p>
26+
27+
*/
28+
public class MockOnlyRule implements TestRule {
29+
30+
final String downloader = System.getProperty("downloader");
31+
32+
@Override
33+
@Nonnull
34+
public Statement apply(@Nonnull Statement base, @Nonnull Description description) {
35+
return new Statement() {
36+
@Override
37+
public void evaluate() throws Throwable {
38+
final MockOnly annotation = description.getAnnotation(MockOnly.class);
39+
if (annotation != null) {
40+
final boolean isMockDownloader = downloader == null ||
41+
!downloader.equalsIgnoreCase(DownloaderType.REAL.toString());
42+
43+
Assume.assumeTrue("The test is not reliable against real website. Reason: "
44+
+ annotation.reason(), isMockDownloader);
45+
}
46+
47+
base.evaluate();
48+
}
49+
};
50+
}
51+
}

0 commit comments

Comments
 (0)