[test] Print Test Method Name for failed rule tests

This helps to select just this failed test to
rerun it within the IDE.
Also replace parenthesis in the test case description.
This commit is contained in:
Andreas Dangel
2022-04-24 22:26:46 +02:00
parent 24f4b2ecb6
commit 1f9c7c5765
4 changed files with 21 additions and 4 deletions

View File

@ -42,6 +42,8 @@ public class PMDTestRunner extends Runner implements Filterable, Sortable {
private final RuleTestRunner ruleTests;
private final ParentRunner<?> unitTests;
private final Description description;
public PMDTestRunner(final Class<? extends SimpleAggregatorTst> klass) throws InitializationError {
this.klass = klass;
ruleTests = new RuleTestRunner(klass);
@ -51,6 +53,8 @@ public class PMDTestRunner extends Runner implements Filterable, Sortable {
} else {
unitTests = new EmptyRunner(klass);
}
this.description = createDescription();
}
@Override
@ -76,6 +80,10 @@ public class PMDTestRunner extends Runner implements Filterable, Sortable {
@Override
public Description getDescription() {
return description;
}
private Description createDescription() {
Description description = Description.createSuiteDescription(klass);
description.addChild(createChildrenDescriptions(ruleTests, "Rule Tests"));
if (ruleTests.hasUnitTests()) {

View File

@ -47,10 +47,7 @@ public class RuleTestRunner extends ParentRunner<TestDescriptor> {
protected Description describeChild(TestDescriptor testCase) {
Description description = testDescriptions.get(testCase);
if (description == null) {
description = Description.createTestDescription(getTestClass().getJavaClass(),
testCase.getRule().getName() + "::"
+ testCase.getNumberInDocument() + " "
+ testCase.getDescription().replaceAll("\n|\r", " "));
description = Description.createTestDescription(getTestClass().getJavaClass().getName(), testCase.getTestMethodName());
testDescriptions.putIfAbsent(testCase, description);
}
return description;

View File

@ -239,6 +239,7 @@ public abstract class RuleTst {
+ " problem(s) found.");
System.out.println(" -> Expected messages: " + test.getExpectedMessages());
System.out.println(" -> Expected line numbers: " + test.getExpectedLineNumbers());
System.out.println("Test Method Name: " + test.getTestMethodName());
System.out.println();
TextRenderer renderer = new TextRenderer();
renderer.setWriter(new StringWriter());

View File

@ -148,4 +148,15 @@ public class TestDescriptor {
public boolean isUseAuxClasspath() {
return useAuxClasspath;
}
public String getTestMethodName() {
String methodName = getRule().getName() + "_"
+ getNumberInDocument()
+ "_"
+ getDescription()
.replaceAll("\n|\r", "_")
.replaceAll("[\\(\\)]|\\s", "_");
return methodName;
}
}