Skip to content

Commit

Permalink
[junit] support custom parameterized runners to run single test metho…
Browse files Browse the repository at this point in the history
…ds from editor directly (IDEA-275586)

GitOrigin-RevId: c8a8f9d9d401f0f360718fea2b46bfce7d1f6904
  • Loading branch information
akozlova authored and intellij-monorepo-bot committed Aug 16, 2021
1 parent 857b57a commit fc1aaf2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
Expand Up @@ -57,19 +57,28 @@ protected void setupModule() throws Exception {

@Test
public void executeOneParameter() throws ExecutionException {
ProcessOutput processOutput = doStartProcess();
ProcessOutput processOutput = doStartProcess(myParamString);
String testOutput = processOutput.out.toString();
assertEmpty(processOutput.err);
assertTrue(testOutput, testOutput.contains("Test1"));
assertFalse(testOutput, testOutput.contains("Test2"));
}

private ProcessOutput doStartProcess() throws ExecutionException {
@Test
public void executeNoParameters() throws ExecutionException {
ProcessOutput processOutput = doStartProcess(null);
String testOutput = processOutput.out.toString();
assertEmpty(processOutput.err);
assertTrue(testOutput, testOutput.contains("Test1"));
assertTrue(testOutput, testOutput.contains("Test2"));
}

private ProcessOutput doStartProcess(String paramString) throws ExecutionException {
PsiClass psiClass = findClass(myModule, CLASS_NAME);
assertNotNull(psiClass);
PsiMethod testMethod = psiClass.findMethodsByName(METHOD_NAME, false)[0];
JUnitConfiguration configuration = createConfiguration(testMethod);
configuration.setProgramParameters(myParamString);
configuration.setProgramParameters(paramString);
return doStartTestsProcess(configuration);
}
}
23 changes: 16 additions & 7 deletions plugins/junit_rt/src/com/intellij/junit4/JUnit4TestRunnerUtil.java
Expand Up @@ -183,6 +183,10 @@ public boolean shouldRun(Description description) {
if (description.isTest() && description.getDisplayName().startsWith("warning(junit.framework.TestSuite$")) {
return true;
}

if (description.isTest() && isParameterizedMethodName(description.getMethodName(), methodName)) {
return true;
}

return methodFilter.shouldRun(description);
}
Expand Down Expand Up @@ -229,11 +233,11 @@ private static boolean isParameterized(final String methodName,
return true;
}
if (methodName != null) {
try {
Method method = clazz.getMethod(methodName);
return method.getParameterTypes().length > 0;
for (Method method : clazz.getDeclaredMethods()) {
if (methodName.equals(method.getName()) && method.getParameterTypes().length > 0) {
return true;
}
}
catch (NoSuchMethodException ignored) { }
}
return false;
}
Expand Down Expand Up @@ -270,9 +274,7 @@ public boolean shouldRun(Description description) {
//filter only selected method
if (methodName != null && descriptionMethodName != null &&
!descriptionMethodName.equals(methodName) && //If fork mode is used, a parameter is included in the name itself
!(descriptionMethodName.startsWith(methodName) &&
//methodName[ valid for any parameter for the current method.
descriptionMethodName.length() > methodName.length() && descriptionMethodName.substring(methodName.length()).trim().startsWith("["))) {
!isParameterizedMethodName(descriptionMethodName, methodName)) {
return false;
}
return true;
Expand All @@ -297,6 +299,13 @@ public String describe() {
return null;
}

private static boolean isParameterizedMethodName(String parameterizedMethodName, String baseMethodName) {
return parameterizedMethodName.startsWith(baseMethodName) &&
//methodName[ valid for any parameter for the current method.
parameterizedMethodName.length() > baseMethodName.length() &&
parameterizedMethodName.substring(baseMethodName.length()).trim().startsWith("[");
}

private static Request getClassRequestsUsing44API(String suiteName, Class<?>[] classes) {
Request allClasses;
try {
Expand Down

0 comments on commit fc1aaf2

Please sign in to comment.