Skip to content

Commit

Permalink
Support filters for parameterized tests.
Browse files Browse the repository at this point in the history
Previously attempting to run only a single variant of parameterized test:
eg 'instrument ... -e class FooTest#bar[0]'. would be silently ignored
with 0 tests run.

This change adjusts the AJUR classmethod filter, and the corresponding testFile
parser to support filtering individual parameterized test variants.

PiperOrigin-RevId: 312342209
  • Loading branch information
brettchabot authored and copybara-androidxtest committed May 19, 2020
1 parent bbc35ad commit 8064ecd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ public class RunnerArgs {
private static final String CLASSPATH_SEPARATOR = ":";
// used to separate fully-qualified test case class name, and one of its methods
private static final char METHOD_SEPARATOR = '#';
// pattern used to identify java class names conforming to java naming conventions
private static final String CLASS_OR_METHOD_REGEX =
"^([\\p{L}_$][\\p{L}\\p{N}_$]*\\.)*[\\p{Lu}_$][\\p{L}\\p{N}_$]*(#[\\p{L}_$][\\p{L}\\p{N}_$]*)?$";

public final boolean debug;
public final boolean suiteAssignment;
Expand Down Expand Up @@ -489,7 +486,13 @@ private BufferedReader openFile(Instrumentation instr, String filePath) throws I
*/
@VisibleForTesting
static boolean isClassOrMethod(String line) {
return line.matches(CLASS_OR_METHOD_REGEX);
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if (c == '#' || Character.isUpperCase(c)) {
return true;
}
}
return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,14 +467,16 @@ public boolean evaluateTest(Description description) {

// Parameterized tests append "[#]" at the end of the method names.
// For instance, "getFoo" would become "getFoo[0]".
methodName = stripParameterizedSuffix(methodName);
if (excludedMethods.contains(methodName)) {
// Method filters should be applied against both the parameterized name and root name
String rootMethodName = stripParameterizedSuffix(methodName);
if (excludedMethods.contains(methodName) || excludedMethods.contains(rootMethodName)) {
return false;
}
// don't filter out descriptions with method name "initializationError", since
// Junit will generate such descriptions in error cases, See ErrorReportingRunner
return includedMethods.isEmpty()
|| includedMethods.contains(methodName)
|| includedMethods.contains(rootMethodName)
|| methodName.equals("initializationError");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,26 +391,22 @@ public void isClassOrMethod() throws Exception {
assertTrue(RunnerArgs.Builder.isClassOrMethod("Bar"));
assertTrue(RunnerArgs.Builder.isClassOrMethod("Bar#method_$1"));
assertTrue(RunnerArgs.Builder.isClassOrMethod("pkg.foo_1.foo$.Bar2#m"));

// test parameterized name
assertTrue(RunnerArgs.Builder.isClassOrMethod("pkg.foo.Bar#method[0]"));
}

/**
* Test static method isClassOrMethod in RunnerArgs.Builder with package names or invalid names
*/
/** Test static method isClassOrMethod in RunnerArgs.Builder */
@Test
public void notClassOrMethod() throws Exception {
// test valid package names
assertFalse(RunnerArgs.Builder.isClassOrMethod("pkg.foo.bar"));
assertFalse(RunnerArgs.Builder.isClassOrMethod("pkg"));
assertFalse(RunnerArgs.Builder.isClassOrMethod("pkg1.foo_2.bar$3"));

// invalid names should not register as class or method names
assertFalse(RunnerArgs.Builder.isClassOrMethod(""));
assertFalse(RunnerArgs.Builder.isClassOrMethod(".pkg.foo.Bar"));
assertFalse(RunnerArgs.Builder.isClassOrMethod("pkg..foo.Bar"));
assertFalse(RunnerArgs.Builder.isClassOrMethod("pkg.foo.Bar."));
assertFalse(RunnerArgs.Builder.isClassOrMethod("2pkg.foo.Bar"));
assertFalse(RunnerArgs.Builder.isClassOrMethod("pkg.foo.Bar#method.1"));
assertFalse(RunnerArgs.Builder.isClassOrMethod("pkg.foo.Bar#method1%"));
assertFalse(RunnerArgs.Builder.isClassOrMethod("pkg.foo&.Bar#method1"));
assertFalse(RunnerArgs.Builder.isClassOrMethod("$^$%^"));
}

/** Test parsing bundle when test timeout is provided */
Expand Down

0 comments on commit 8064ecd

Please sign in to comment.