mirror of
https://github.com/TeamNewPipe/NewPipeExtractor.git
synced 2024-12-14 22:30:33 +05:30
Add MockOnlyRule to allow skipping specific tests based on downloader
This commit is contained in:
parent
27a20e41cd
commit
ea52030613
18
extractor/src/test/java/org/schabi/newpipe/MockOnly.java
Normal file
18
extractor/src/test/java/org/schabi/newpipe/MockOnly.java
Normal file
@ -0,0 +1,18 @@
|
||||
package org.schabi.newpipe;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Marker annotation to skip test if it not run with mocks.
|
||||
*
|
||||
* {@link MockOnlyRule}
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.METHOD, ElementType.TYPE})
|
||||
@Inherited
|
||||
public @interface MockOnly {
|
||||
}
|
48
extractor/src/test/java/org/schabi/newpipe/MockOnlyRule.java
Normal file
48
extractor/src/test/java/org/schabi/newpipe/MockOnlyRule.java
Normal file
@ -0,0 +1,48 @@
|
||||
package org.schabi.newpipe;
|
||||
|
||||
import org.junit.Assume;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
import org.schabi.newpipe.downloader.DownloaderType;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Checks if the system variable "downloader" is set to "REAL" and skips the tests if it is.
|
||||
* Otherwise execute the test.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Use it by creating a public variable of this inside the test class and annotate it with
|
||||
* {@link org.junit.Rule}. Then annotate the tests to be skipped with {@link MockOnly}
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Allows skipping unreliable or time sensitive tests in CI pipeline.
|
||||
* </p>
|
||||
*/
|
||||
public class MockOnlyRule implements TestRule {
|
||||
|
||||
final String downloader = System.getProperty("downloader");
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public Statement apply(@Nonnull Statement base, @Nonnull Description description) {
|
||||
return new Statement() {
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
final boolean hasAnnotation = description.getAnnotation(MockOnly.class) == null;
|
||||
final boolean isMockDownloader = downloader == null ||
|
||||
!downloader.equalsIgnoreCase(DownloaderType.REAL.toString());
|
||||
Assume.assumeTrue(
|
||||
"The test is not reliable against a website and thus skipped",
|
||||
hasAnnotation && isMockDownloader
|
||||
);
|
||||
|
||||
base.evaluate();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user