Skip to content

Commit d2bc732

Browse files
authored
Fix invalid naming of dynamic tests when executing only a singular test method from the IDE (#323)
1 parent 600cd50 commit d2bc732

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

instrumentation/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Change Log
44
## Unreleased
55

66
- Fix inheritance hierarchy of `ComposeExtension` to avoid false-positive warning regarding `@RegisterExtension` (#318)
7+
- Improve parallel test execution for Android instrumentation tests
8+
- Fix invalid naming of dynamic tests when executing only a singular test method from the IDE (#317)
79

810
## 1.4.0 (2023-11-05)
911

instrumentation/runner/src/main/kotlin/de/mannodermaus/junit5/internal/runners/AndroidJUnitPlatformTestTree.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.junit.platform.launcher.TestPlan
1313
import org.junit.platform.suite.api.SuiteDisplayName
1414
import org.junit.platform.suite.api.UseTechnicalNames
1515
import org.junit.runner.Description
16-
import java.util.*
16+
import java.util.Optional
1717
import java.util.function.Predicate
1818

1919
/**
@@ -52,7 +52,6 @@ internal class AndroidJUnitPlatformTestTree(
5252
nameComponents.add(formatTestName(currentNode))
5353
currentNode = modifiedTestPlan.getRealParent(currentNode).orElse(null)
5454
}
55-
5655
nameComponents.reverse()
5756

5857
// Android's Unified Test Platform (AGP 7.0+) is using literal test names
@@ -66,12 +65,15 @@ internal class AndroidJUnitPlatformTestTree(
6665
}
6766

6867
private fun formatTestName(identifier: TestIdentifier): String {
69-
// During isolated executions, construct a technical version of the test name
70-
// for backwards compatibility with the JUnit 4-based instrumentation of Android,
71-
// stripping the brackets and parameters completely.
72-
// If we didn't, then running them from the IDE doesn't work for @Test methods with parameters
73-
// (See AndroidX's TestRequestBuilder$MethodFilter for where this is cross-referenced).
74-
if (isIsolatedMethodRun) {
68+
// During isolated executions of non-dynamic @Test methods,
69+
// construct a technical version of the test name for backwards compatibility
70+
// with the JUnit 4-based instrumentation of Android by stripping the brackets and parameters completely.
71+
// If this didn't happen, running them from the IDE will cause "No tests found" errors.
72+
// See AndroidX's TestRequestBuilder$MethodFilter for where this is cross-referenced in the instrumentation!
73+
//
74+
// Ref issues #199 & #207 (the original unearthing of this behavior),
75+
// as well as #317 (making an exception for dynamic tests).
76+
if (isIsolatedMethodRun && !identifier.isDynamicTest) {
7577
val reportName = identifier.legacyReportingName
7678
val bracketIndex = reportName.indexOf('(')
7779
if (bracketIndex > -1) {

instrumentation/sample/src/androidTest/kotlin/de/mannodermaus/sample/ActivityOneTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import de.mannodermaus.junit5.condition.EnabledIfBuildConfigValue
1111
import de.mannodermaus.junit5.sample.ActivityOne
1212
import de.mannodermaus.junit5.sample.R
1313
import org.junit.jupiter.api.Assertions.assertEquals
14+
import org.junit.jupiter.api.DisplayName
1415
import org.junit.jupiter.api.RepeatedTest
1516
import org.junit.jupiter.api.RepetitionInfo
1617
import org.junit.jupiter.api.Tag
@@ -25,6 +26,12 @@ class ActivityOneTest {
2526
@RegisterExtension
2627
val scenarioExtension = ActivityScenarioExtension.launch<ActivityOne>()
2728

29+
@DisplayName("test with pretty display name")
30+
@Test
31+
fun testDisplayName() {
32+
onView(withId(R.id.textView)).check(matches(withText("0")))
33+
}
34+
2835
@EnabledIfBuildConfigValue(named = "MY_VALUE", matches = "true")
2936
@Test
3037
fun testExample(scenario: ActivityScenario<ActivityOne>) {

0 commit comments

Comments
 (0)