Skip to content

Add junitSelectors instrumentation argument for batched method and invocation selection - #416

Open
GayatriNairAtBrowserstack wants to merge 1 commit into
mannodermaus:mainfrom
GayatriNairAtBrowserstack:junit-selectors-argument
Open

GayatriNairAtBrowserstack wants to merge 1 commit into
mannodermaus:mainfrom
GayatriNairAtBrowserstack:junit-selectors-argument

Conversation

@GayatriNairAtBrowserstack

Copy link
Copy Markdown

Problem

The class instrumentation argument is read by two components at once: AndroidX's TestRequestBuilder and this runner's ParsedSelectors. AndroidX applies its method filter to the reported test names, which for JUnit Jupiter are display names (display named failing, param(int) - [2] 2). So:

  • Selecting more than one method of the same class in a single run, e.g. -e class 'com.Foo#a,com.Foo#b', filters out the whole class, and 0 tests run. This holds whenever a selected method's display name differs from its Java name, including parameterized and repeated tests.
  • A single invocation of a parameterized or repeated test cannot be selected at all.
  • Running one method at a time works, because the runner switches to the legacy name format, but it costs one am instrument start-up per method.

This matters for tools that re-run failed tests: they want one instrumentation run for all failures, with names that match the original run.

Change

A new instrumentation argument, junitSelectors, read only by this runner:

-e junitSelectors "com.Foo#method,com.Foo#param[2],com.Foo$Inner#test,com.Bar"
  • com.Foo#method selects a method (all overloads with that name)
  • com.Foo#method[n] selects the n-th invocation (1-based, as in the display name) of a @TestTemplate method such as @ParameterizedTest or @RepeatedTest; for other methods the index is ignored
  • com.Foo selects the class
  • com.Foo$Inner#test selects methods of @Nested classes through their enclosing class. AndroidX's class scan does not hand nested classes to the runner on its own
  • Classes that are not mentioned run nothing
  • When this argument is present it takes precedence over class, and the normal (non-legacy) name format is kept, so reported names match a full run

The argument is parsed once in JUnitFrameworkRunnerParams.create(), like configurationParameters, environmentVariables and systemProperties.

Tests

  • New JUnitSelectorsTests (12 cases): several methods of one class, single invocations of parameterized, repeated and nested parameterized tests, nested methods and classes, dynamic tests through their factory, unknown classes, whitespace and duplicates
  • testFiveDebugUnitTest and testSixDebugUnitTest: 109/109 pass
  • Checked on an Android 15 (API 35) emulator with JUnit 6.1.3. One run with 11 selectors across 5 classes, including two @Nested classes and param[2], executed exactly the selected tests, and only invocation 2 of the parameterized test. Reported names matched a full run in both the instrumentation output and logcat.

The new tests are written with JUnit Jupiter, not added to AndroidJUnitFrameworkTests, because that JUnit 4/Robolectric class is not picked up by the current unit test tasks (no vintage engine).

🤖 Generated with Claude Code

…vocation selection

The 'class' instrumentation argument is read by both AndroidX's TestRequestBuilder
and this runner. AndroidX compares its method part against reported test names,
which for JUnit Jupiter are display names, so selecting several methods of one
class in a single run filters out the whole class, and single invocations of
parameterized tests cannot be selected at all.

junitSelectors is read only by this runner: comma-separated com.Foo, com.Foo#method
or com.Foo#method[n] (1-based invocation of a test template), including methods of
@nested classes. Classes not mentioned run nothing, and the normal (non-legacy)
name format is kept, so names match a full run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant