下面列出了org.junit.runner.Description#getDisplayName() 实例代码,或者点击链接到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;
}
private GradingTest parseTest(final Description description, GradingPoints.PointsFormat pointsFormat) {
final GradedTest annotation = description.getAnnotation(GradedTest.class);
if (annotation == null) {
throw new AssertionError("Test missing an annotation: " + description.getDisplayName());
}
if (annotation.name().equals(GradedTest.DEFAULT_NAME)) {
throw new IllegalArgumentException("Test without a name: " + description.getDisplayName());
}
final String testDescription =
annotation.description().equals(GradedTest.DEFAULT_DESCRIPTION) ? null : annotation.description();
final GradingTest test = new GradingTest(annotation.name(), testDescription, annotation.points(), pointsFormat);
currentSuiteTests.put(description, test);
return test;
}
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
if (TestBigQuery.this.datasetService != null) {
throw new AssertionError(
"BigQuery test was not shutdown previously. "
+ "Table is'"
+ table
+ "'. "
+ "Current test: "
+ description.getDisplayName());
}
try {
initializeBigQuery(description);
base.evaluate();
} finally {
tearDown();
}
}
};
}
@Override
public void testIgnored(Description description) {
logger.info(">>> testIgnored called");
printDescription(description, 1);
if (description == null || description.getDisplayName() == null) {
logger.info("<<< testFinished called");
return;
}
TestSuiteResultsStore store = getStore(description);
// create new entry
TestCaseResult testCaseResult = new TestCaseResult(getTestCaseName(description),
getTestSuiteClassName(description),
//new Long(endTimeTestCase - startTimeTestCase).toString(),
"0.000",
"ignored", // status
null, // no failure
null // no systemOut
);
store.testCaseResults.add(testCaseResult);
store.skipped++;
logger.info("<<< testIgnored called");
}
public void testIgnored(Description description) {
logger.info(">>> testIgnored called");
printDescription(description, 1);
if (description == null || description.getDisplayName() == null) {
logger.info("<<< testFinished called");
return;
}
TestSuiteResultsStore store = getStore(description);
// create new entry
TestCaseResult testCaseResult = new TestCaseResult(getTestCaseName(description),
getTestSuiteClassName(description),
//new Long(endTimeTestCase - startTimeTestCase).toString(),
"0.000",
"ignored", // status
null, // no failure
null // no systemOut
);
store.testCaseResults.add(testCaseResult);
store.skipped++;
logger.info("<<< testIgnored called");
}
public static String getMethodName(Description description) {
try {
return description.getMethodName();
}
catch (NoSuchMethodError e) {
final String displayName = description.getDisplayName();
Matcher matcher = Pattern.compile("(.*)\\((.*)\\)").matcher(displayName);
if (matcher.matches()) {
return matcher.group(1);
}
return null;
}
}
/***/
public static String getSuiteName(Description description) {
String text = null;
if (description.isSuite()) {
String s = description.getDisplayName();
int posSemi = s.indexOf(":");
text = s.substring(0, posSemi);
}
return text;
}
@Override
public void testFailure(Failure failure) {
logger.info(">>> testFailure called");
Description description = failure.getDescription();
printDescription(description, 1);
logger.info("- failure.getException() = " + failure.getException());
logger.info("- failure.getMessage() = " + failure.getMessage());
logger.info("- failure.getTestHeader() = " + failure.getTestHeader());
logger.info("- failure.getTrace() = " + failure.getTrace());
if (description == null || description.getDisplayName() == null) {
logger.info("<<< testFinished called");
return;
}
// should have been created already in previous call to testStarted
TestSuiteResultsStore store = getStore(description);
TestCaseFailure testCaseFailure = new TestCaseFailure(failure.getMessage(), // message
failure.getException().toString(), // type
failure.getTrace() // text
);
TestCaseResult testCaseResult = new TestCaseResult(getTestCaseName(description),
getTestSuiteClassName(description),
null, // test not finished yet, will be updated later
"failed", // status
testCaseFailure, // failure
null // no systemOut
);
store.testCaseResults.add(testCaseResult);
store.failures++;
// everything else will be done in testFinished, which is also
// called for failed tests as well.
logger.info("<<< testFailure called");
}
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;
}
/**
* Find feature and story for given scenario
*
* @param scenarioName
* @return {@link String[]} of ["<FEATURE_NAME>", "<STORY_NAME>"]s
* @throws IllegalAccessException
*/
private String[] findFeatureByScenarioName(String scenarioName) throws IllegalAccessException {
List<Description> testClasses = findTestClassesLevel(parentDescription.getChildren());
for (Description testClass : testClasses) {
List<Description> features = findFeaturesLevel(testClass.getChildren());
//Feature cycle
for (Description feature : features) {
//Story cycle
for (Description story : feature.getChildren()) {
Object scenarioType = getTestEntityType(story);
//Scenario
if (scenarioType instanceof Scenario
&& story.getDisplayName().equals(scenarioName)) {
return new String[]{feature.getDisplayName(), scenarioName};
//Scenario Outline
} else if (scenarioType instanceof ScenarioOutline) {
List<Description> examples = story.getChildren().get(0).getChildren();
// we need to go deeper :)
for (Description example : examples) {
if (example.getDisplayName().equals(scenarioName)) {
return new String[]{feature.getDisplayName(), story.getDisplayName()};
}
}
}
}
}
}
return new String[]{"Feature: Undefined Feature", scenarioName};
}
private static GradingSuite parseSuite(final Description description, final List<GradingTest> tests) {
final GradedCategory annotation = description.getAnnotation(GradedCategory.class);
if (annotation == null) {
throw new AssertionError("Category missing an annotation: " + description.getDisplayName());
}
return new GradingSuite(annotation.value(), tests);
}
private static GradingTest parseTest(final Description description, final List<GradingProblem> problems) {
final GradedTest annotation = description.getAnnotation(GradedTest.class);
if (annotation == null) {
throw new AssertionError("Test missing an annotation: " + description.getDisplayName());
}
return new GradingTest(annotation.value(), problems);
}
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;
}
private static String formatDescriptionName(Description description) {
String methodName = (description.getMethodName() == null) ? "" : description.getMethodName();
String className = (description.getClassName() == null) ? "" : description.getClassName();
if (methodName.trim().isEmpty() || className.trim().isEmpty()) {
return description.getDisplayName();
}
return String.format(TEST_NAME_FORMAT, className, methodName);
}
public String getTestCaseName(Description description) {
String baseTestClassName;
Pattern pattern = Pattern.compile("(.*)\\(");
Matcher matcher = pattern.matcher(description.getDisplayName());
if (matcher.find()) {
baseTestClassName = matcher.group(1);
} else {
baseTestClassName = description.getDisplayName();
}
return baseTestClassName;
}
private String parseDescription(Description description) {
String displayName = description.getDisplayName();
int p1 = displayName.indexOf("(");
int p2 = displayName.indexOf(")");
if (p1 < 0 || p2 < 0 || p2 <= p1) {
return displayName;
}
String methodName = displayName.substring(0, p1);
String className = displayName.substring(p1 + 1, p2);
return replaceAll(className) + " " + methodName;
}
public String getTestCaseName(Description description) {
String baseTestClassName;
Pattern pattern = Pattern.compile("(.*)\\(");
Matcher matcher = pattern.matcher(description.getDisplayName());
if (matcher.find()) {
baseTestClassName = matcher.group(1);
} else {
baseTestClassName = description.getDisplayName();
}
return baseTestClassName;
}
private static Description wrap(Description description) {
String name = description.getDisplayName();
String fixedName = deparametrizedName(name);
Description clonedDescription = Description.createSuiteDescription(fixedName, description.getAnnotations()
.toArray(new Annotation[0]));
for (Description child : description.getChildren()) {
clonedDescription.addChild(wrap(child));
}
return clonedDescription;
}
@Override
public void testFinished(Description description) {
logger.info(">>> testFinished called");
printDescription(description, 1);
if (description == null || description.getDisplayName() == null) {
logger.info("<<< testFinished called");
return;
}
TestSuiteResultsStore store = getStore(description);
long endTimeTestCase = System.currentTimeMillis();
// this case may have failed and then we already have an entry.
// check for description in list
boolean found = false;
for (int i = 0; i < store.testCaseResults.size(); i++) {
if (store.testCaseResults.get(i).getName().equals(getTestCaseName(description))) {
// update time for failed entry
//testCaseResults.get(i).setTime(new Long(endTimeTestCase - startTimeTestCase).toString());
store.testCaseResults.get(i).setTime(getFormattedDuration(endTimeTestCase - startTimeTestCase));
found = true;
break;
}
}
if (found == false) {
// create new entry
TestCaseResult testCaseResult = new TestCaseResult(getTestCaseName(description),
getTestSuiteClassName(description),
//new Long(endTimeTestCase - startTimeTestCase).toString(),
getFormattedDuration(endTimeTestCase
- startTimeTestCase),
"ok", // status
null, // no failure
null // no systemOut
);
store.testCaseResults.add(testCaseResult);
}
store.tests++;
store.consumedTime += (endTimeTestCase - startTimeTestCase);
logger.info("<<< testFinished called");
}
public void testFinished(Description description) {
logger.info(">>> testFinished called");
printDescription(description, 1);
if (description == null || description.getDisplayName() == null) {
logger.info("<<< testFinished called");
return;
}
TestSuiteResultsStore store = getStore(description);
long endTimeTestCase = System.currentTimeMillis();
// this case may have failed and then we already have an entry.
// check for description in list
boolean found = false;
for (int i = 0; i < store.testCaseResults.size(); i++) {
if (store.testCaseResults.get(i).getName().equals(getTestCaseName(description))) {
// update time for failed entry
//testCaseResults.get(i).setTime(new Long(endTimeTestCase - startTimeTestCase).toString());
store.testCaseResults.get(i).setTime(getFormattedDuration(endTimeTestCase - startTimeTestCase));
found = true;
break;
}
}
if (found == false) {
// create new entry
TestCaseResult testCaseResult = new TestCaseResult(getTestCaseName(description),
getTestSuiteClassName(description),
//new Long(endTimeTestCase - startTimeTestCase).toString(),
getFormattedDuration(endTimeTestCase
- startTimeTestCase),
"ok", // status
null, // no failure
null // no systemOut
);
store.testCaseResults.add(testCaseResult);
}
store.tests++;
store.consumedTime += (endTimeTestCase - startTimeTestCase);
logger.info("<<< testFinished called");
}