下面列出了org.junit.runner.Description#isTest() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/***/
public static String getTestName(Description description) {
String text = null;
if (description.isTest()) {
String s = description.getDisplayName();
if (s.startsWith(TEST_FILE_INIT_ERROR_MSG)) {
return TEST_FILE_INIT_ERROR_MSG;
}
// seems like malformed xt file - no XPECT comment ?
if (s.indexOf("#") < 0 || s.indexOf("~") < 0) {
return s;
}
int posXT = s.indexOf("#");
int posTM = s.indexOf("~", posXT);
text = s.substring(posXT + 1, posTM);
}
return text;
}
/**
* Given a list of test case descriptions, returns a mapping from each
* to its index in the list.
*/
private static Map<Description, Integer> buildTestToShardMap(
Collection<Description> testDescriptions) {
Map<Description, Integer> map = new HashMap<>();
// Sorting this list is incredibly important to correctness. Otherwise,
// "shuffled" suites would break the sharding protocol.
List<Description> sortedDescriptions = new ArrayList<>(testDescriptions);
Collections.sort(sortedDescriptions, new DescriptionComparator());
// If we get two descriptions that are equal, the shard number for the second
// one will overwrite the shard number for the first. Thus they'll run on the
// same shard.
int index = 0;
for (Description description : sortedDescriptions) {
if (!description.isTest()) {
throw new IllegalArgumentException("Test suite should not be included in the set of tests "
+ "to shard: " + description.getDisplayName());
}
map.put(description, index);
index++;
}
return Collections.unmodifiableMap(map);
}
@Override
public String getText(Object element) {
if (element instanceof Description == false) {
return "";
}
Description desc = ((Description) element);
if (desc.isSuite()) {
return N4IDEXpectFileNameUtil.getSuiteName(desc);
}
if (desc.isTest()) {
return N4IDEXpectFileNameUtil.getTestName(desc);
}
return "";
}
/**
* get icon based on item type (test/suite) and its status (pass/failed/exception/skip/in progress...)
*/
private ImageDescriptor getImageDescriptor(Object element) throws RuntimeException {
ImageDescriptor descriptor = null;
if (element instanceof Description == false) {
String msg = "Unknown type of element in tree of type " + element.getClass().getName();
Exception e = new RuntimeException(msg);
N4IDEXpectUIPlugin.logError("cannot obtain image descriptor, fallback to default", e);
return getImageDescriptor("n4_logo.png");
}
Description desc = (Description) element;
if (desc.isTest()) {
descriptor = getTestImageDescriptor(executionStatus.getStatus(desc));
} else if (desc.isSuite()) {
descriptor = getSuiteImageDescriptor(desc, executionStatus.getStatus(desc));
} else {
descriptor = getImageDescriptor("n4_logo.png");
}
return descriptor;
}
@Override
public Statement apply(final Statement base, final Description description) {
if (!description.isTest()) return base;
final String contextPlatform = SeleniumConfig.getConfig().getContextPlatform();
final TargetPlatform targetPlatform = description.getAnnotation(TargetPlatform.class);
platform = TargetPlatformHandler.resolveTargetPlatform(testObject, targetPlatform);
if (TargetPlatformHandler.shouldRun(contextPlatform, platform)) {
return base;
} else {
return new Statement() {
@Override
public void evaluate() throws Throwable {
String message = String.format("%s.%s() doesn't specify platform '%s'",
description.getClassName(), description.getMethodName(), contextPlatform);
throw new AssumptionViolatedException(message);
}
};
}
}
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
if (description.isTest()) {
if (RequiresRedisSentinel.this.requiredSentinels != null) {
verify(RequiresRedisSentinel.this.requiredSentinels);
}
} else {
verify(RequiresRedisSentinel.this.requiredSentinels);
}
base.evaluate();
}
};
}
@Override
public boolean shouldRun(Description description) {
String filterDescription = filter.describe();
Matcher filterDescriptionMatcher = DESCRIPTION_PATTERN.matcher(filterDescription);
if (filterDescription.contains(" OR ") || !filterDescriptionMatcher.find()) {
return filter.shouldRun(description);
}
String methodName = filterDescriptionMatcher.group(GROUP_METHOD_NAME);
String className = filterDescriptionMatcher.group(GROUP_CLASS);
if (description.isTest()) {
return shouldRunTest(description, filterDescriptionMatcher, methodName, className);
}
// explicitly check if any children should to run
for (Description each : description.getChildren()) {
if (shouldRun(each)) {
return true;
}
}
return false;
}
/** {@inheritDoc} */
@Override
public boolean shouldRun(Description description) {
if (description.isTest()) {
return evaluateTest(description);
}
// this is a suite, explicitly check if any children should run
for (Description each : description.getChildren()) {
if (shouldRun(each)) {
return true;
}
}
// no children to run, filter this out
return false;
}
public void startFakeTestCase(Description description) {
String uid = getSuiteUid(description);
String name = description.isTest() ? getTestName() : getSuiteName(description);
TestCaseStartedEvent event = new TestCaseStartedEvent(uid, name);
AnnotationManager am = new AnnotationManager(description.getAnnotations());
am.update(event);
fireClearStepStorage();
getLifecycle().fire(event);
}
@Override
public void testStarted(final Description description) {
if (description.isTest() && isGradable(description)) {
currentTest = currentSuiteTests.get(description);
} else {
currentTest = null;
}
}
private GradingSuite parseSuite(final Description description) {
final GradedCategory annotation = description.getAnnotation(GradedCategory.class);
final String suiteName =
annotation.name().equals(GradedTest.DEFAULT_NAME) ?
description.getDisplayName() : annotation.name();
final String suiteDescription =
annotation.description().equals(GradedTest.DEFAULT_DESCRIPTION) ?
null : annotation.description();
final String suiteGroup =
annotation.group().equals(GradedCategory.DEFAULT_GROUP) ?
null : annotation.group();
final GradingSuite suite = new GradingSuite(description.getClassName(), suiteName,
suiteDescription, suiteGroup, annotation.pointsFormat());
for (final Description child : description.getChildren()) {
if (isGradable(child)) {
if (child.isTest()) {
suite.tests.add(parseTest(child, annotation.pointsFormat()));
} else {
throw new AssertionError("Non-test child. How did you even do that?");
}
}
}
return suite;
}
@Override
public void testStarted(final Description description) {
if (description.isTest() && isGradable(description)) {
currentProblems = new ArrayList<>();
} else {
currentProblems = null;
}
}
@Override
public void testStarted(final Description description) {
if (description.isTest() && isGradable(description)) {
currentTest = currentSuiteTests.get(description);
} else {
currentTest = null;
}
}
/**
* Constructs a {@code DynamicTestFailureException} that indicates a
* dynamically-discovered test, specified as a (@link Description}, failed
* due to the specified {@code cause}.
*/
public DynamicTestException(Description test, Throwable cause) {
super(cause);
if (!test.isTest()) {
throw new IllegalArgumentException();
}
this.test = test;
}
/**
* Validates the annotation of the rule field in the test class.
*
* @param description
*/
private void validateRuleAnnotations(Description description) {
// If the first run is a @ClassRule run, check if @Rule is annotated
if (firstRun && !description.isTest()) {
/*
* Get the fields of the test class and check if there is only one
* coverage rule and if the coverage rule field is annotation with
* both @ClassRule and @Rule.
*/
int numberOfCoverageRules = 0;
for (Field field : description.getTestClass().getFields()) {
final Class<?> fieldType = field.getType();
if (getClass().isAssignableFrom(fieldType)) {
++numberOfCoverageRules;
final boolean isClassRule = field.isAnnotationPresent(ClassRule.class);
final boolean isRule = field.isAnnotationPresent(Rule.class);
if (isClassRule && !isRule) {
throw new RuntimeException(getClass().getCanonicalName()
+ " can only be used as a @ClassRule if it is also a @Rule!");
}
}
}
// TODO if they really want to have multiple runs, let them?
if (numberOfCoverageRules > 1) {
throw new RuntimeException("Only one coverage rule can be used per test class!");
}
}
}
@Override
public boolean shouldRun(Description description) {
if (description.isTest()) {
return (Math.abs(description.hashCode()) % numShards) == shardIndex;
}
// The description is a suite, so assume that it can be run so that filtering is
// applied to its children. If after filtering it has no children then it will be
// automatically filtered out.
return true;
}
public void startFakeTestCase(Description description) throws IllegalAccessException {
String uid = getSuiteUid(description);
String name = description.isTest() ? description.getMethodName() : description.getClassName();
TestCaseStartedEvent event = new TestCaseStartedEvent(uid, name);
event.setTitle(name);
AnnotationManager am = new AnnotationManager(description.getAnnotations());
am.update(event);
getLifecycle().fire(event);
}
private void addTestCase(TestSuiteNode parentSuite, Description testCaseDesc) {
if (!testCaseDesc.isTest()) {
throw new IllegalArgumentException();
}
if (!shardingFilter.shouldRun(testCaseDesc)) {
return;
}
TestCaseNode testCase = new TestCaseNode(testCaseDesc, parentSuite);
testsMap.put(testCaseDesc, testCase);
parentSuite.addTestCase(testCase);
}
public void addTests(Description description) {
if (description.isEmpty()) {
return;
}
if (description.isTest()) {
addTest(description.getClassName() + "#" + description.getMethodName());
} else {
for (Description child : description.getChildren()) {
addTests(child);
}
}
}
/**
* If the rule is a @ClassRule log and assert the coverage and create a
* graphical report. For the class coverage to work all the test method
* deployments have to be equal.
*
* @param description
*/
private void handleClassCoverage(Description description) {
// If the rule is a class rule get the class coverage
if (!description.isTest()) {
final ClassCoverage classCoverage = coverageTestRunState.getClassCoverage();
// Make sure the class coverage deals with the same deployments for
// every test method
classCoverage.assertAllDeploymentsEqual();
final double classCoveragePercentage = classCoverage.getCoveragePercentage();
// Log coverage percentage
logger.info(
coverageTestRunState.getTestClassName() + " test class coverage is: " + classCoveragePercentage);
logCoverageDetail(classCoverage);
// Create graphical report
CoverageReportUtil.createClassReport(processEngine, coverageTestRunState);
assertCoverage(classCoveragePercentage, classCoverageAssertionMatchers);
}
}