Support @Skip on test classes and methods

Add super class for all test types (method, scenario, end-to-end),
and an class & method level annotation for skipping tests.
This commit is contained in:
ghubstan 2020-07-12 21:44:21 -03:00
parent 390cba1b75
commit 96cabfb177
No known key found for this signature in database
GPG key ID: E35592D6800A861E
2 changed files with 119 additions and 0 deletions

View file

@ -0,0 +1,81 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.apitest;
import java.util.function.Predicate;
import java.lang.reflect.Method;
import static java.lang.String.format;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import bisq.apitest.annotation.Skip;
public class ApiTestCase {
protected static final char CHECK = '\u2714';
protected static final char CROSS_MARK = '\u274c';
public int countTestCases;
public int countFailedTestCases;
public int countSkippedTestCases;
public int countPassedTestCases;
private final Predicate<Class<?>> skipAll = (c) -> c.getAnnotation(Skip.class) != null;
private final Predicate<Method> skip = (m) -> m.getAnnotation(Skip.class) != null;
protected boolean isSkipped(String methodName) {
try {
if (skipAll.test(this.getClass()) || skip.test(getMethod(methodName))) {
countSkippedTestCases++;
return true;
} else {
return false;
}
} finally {
countTestCases++; // Increment the test case count, skipped or not.
}
}
protected Method getMethod(String methodName) {
try {
return this.getClass().getMethod(methodName);
} catch (NoSuchMethodException ex) {
throw new IllegalStateException(format("No method '%s' exists in class '%s'",
methodName, this.getClass().getName()),
ex);
}
}
protected String reportString() {
return format("Total %d Passed %d Failed %d Skipped %d",
countTestCases,
countPassedTestCases,
countFailedTestCases,
countSkippedTestCases);
}
protected void sleep(long ms) {
try {
MILLISECONDS.sleep(ms);
} catch (InterruptedException ignored) {
}
}
}

View file

@ -0,0 +1,38 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.apitest.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Sometimes you want to temporarily disable a test or a group of tests. Methods that
* are annotated with <code>&#064;Skip</code> will not be executed as tests.
* Also, you can annotate a class containing test methods with <code>&#064;Skip</code>
* and none of the containing tests will be executed.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Skip {
/**
* The optional reason why the test is skipped.
*/
String value() default "";
}