Skip to content

Commit

Permalink
Stop adding a trailing # when filtering tests for JUnit 5
Browse files Browse the repository at this point in the history
The JUnit 5 selector for methods does not support a trailing #.

This is easy to verify:

```
> java -jar junit-platform-console-standalone-<version>.jar execute -cp <my tests jar> --select-class=<my test class>
```

will work, while

```
> java -jar junit-platform-console-standalone-<version>.jar execute -cp <my tests jar> --select-class=<my test class>#
```

will fail.

This will change `--test_filter`'s value from `<my test class>#` to `<my test class>` when filtering for classes. The new value requires no postprocessing and is directly compatible with JUnit's selector "language".

The trailing # was introduced in #4473. This undocumented breaking change created issues for our own test runner.
  • Loading branch information
joca-bt committed Nov 26, 2024
1 parent 97b3ccf commit 33b240b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ static String testFilterForClassAndMethods(
}
String methodNamePattern = concatenateMethodNames(methodFilters, jUnitVersion);
if (Strings.isNullOrEmpty(methodNamePattern)) {
if (jUnitVersion == JUnitVersion.JUNIT_4 || jUnitVersion == JUnitVersion.JUNIT_5) {
if (jUnitVersion == JUnitVersion.JUNIT_4) {
output.append('#');
}
return output.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ public void testConfigurationCreatedFromAbstractClass() throws Throwable {

assertThat(blazeConfig.getTargets())
.containsExactly(TargetExpression.fromStringSafe("//java/com/google/test:TestClass"));
String junit4Hash = (jUnitVersionUnderTest == JUnitVersion.JUNIT_4 ? "#" : "");
assertThat(getTestFilterContents(blazeConfig))
.isEqualTo(BlazeFlags.TEST_FILTER + "=com.google.test.TestClass#");
.isEqualTo(BlazeFlags.TEST_FILTER + "=com.google.test.TestClass" + junit4Hash);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.idea.blaze.base.model.primitives.TargetExpression;
import com.google.idea.blaze.base.model.primitives.WorkspacePath;
import com.google.idea.blaze.base.run.BlazeCommandRunConfiguration;
import com.google.idea.blaze.java.run.producers.BlazeJUnitTestFilterFlags.JUnitVersion;
import com.google.idea.blaze.base.run.producers.TestContextRunConfigurationProducer;
import com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState;
import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager;
Expand Down Expand Up @@ -69,7 +70,9 @@ public void testProducedFromPsiFile() throws Throwable {
(BlazeCommandRunConfiguration) fromContext.getConfiguration();
assertThat(config.getTargets())
.containsExactly(TargetExpression.fromStringSafe("//java/com/google/test:TestClass"));
assertThat(getTestFilterContents(config)).isEqualTo("--test_filter=com.google.test.TestClass#");
String junit4Hash = (jUnitVersionUnderTest == JUnitVersion.JUNIT_4 ? "#" : "");
assertThat(getTestFilterContents(config))
.isEqualTo("--test_filter=com.google.test.TestClass" + junit4Hash);
assertThat(config.getName()).isEqualTo("Bazel test TestClass");
assertThat(getCommandType(config)).isEqualTo(BlazeCommandName.TEST);
}
Expand All @@ -94,7 +97,9 @@ public void testProducedFromPsiClass() throws Throwable {
(BlazeCommandRunConfiguration) fromContext.getConfiguration();
assertThat(config.getTargets())
.containsExactly(TargetExpression.fromStringSafe("//java/com/google/test:TestClass"));
assertThat(getTestFilterContents(config)).isEqualTo("--test_filter=com.google.test.TestClass#");
String junit4Hash = (jUnitVersionUnderTest == JUnitVersion.JUNIT_4 ? "#" : "");
assertThat(getTestFilterContents(config))
.isEqualTo("--test_filter=com.google.test.TestClass" + junit4Hash);
assertThat(config.getName()).isEqualTo("Bazel test TestClass");
assertThat(getCommandType(config)).isEqualTo(BlazeCommandName.TEST);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,14 @@ public void testProducedFromTestFiles() throws Throwable {
// THEN expect config to be correct
assertThat(config.getTargets())
.containsExactly(TargetExpression.fromStringSafe("//java/com/google/test:allTests"));
String junit4Hash = (jUnitVersionUnderTest == JUnitVersion.JUNIT_4 ? "#" : "");
assertThat(getTestFilterContents(config))
.isEqualTo("--test_filter=\"com.google.test.TestClass1#|com.google.test.TestClass2#\"");
.isEqualTo(
"--test_filter=\"com.google.test.TestClass1"
+ junit4Hash
+ "|com.google.test.TestClass2"
+ junit4Hash
+ "\"");
assertThat(config.getName()).isEqualTo("Bazel test TestClass1 and 1 others");
assertThat(getCommandType(config)).isEqualTo(BlazeCommandName.TEST);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void testSingleJUnit5ClassFilter() {
assertThat(
BlazeJUnitTestFilterFlags.testFilterForClassAndMethods(
"com.google.idea.ClassName", JUnitVersion.JUNIT_5, ImmutableList.of(), null))
.isEqualTo("com.google.idea.ClassName#");
.isEqualTo("com.google.idea.ClassName");
}

@Test
Expand Down Expand Up @@ -82,7 +82,7 @@ public void testParameterizedIgnoredForSingleClassJUnit5() {
JUnitVersion.JUNIT_5,
ImmutableList.of(),
ParameterizedTestInfo.create("ignored", "-ignored")))
.isEqualTo("com.google.idea.ClassName#");
.isEqualTo("com.google.idea.ClassName");
}

@Test
Expand Down

0 comments on commit 33b240b

Please sign in to comment.