Skip to content

Apostrophe in display name breaks name generation for @ParameterizedTestΒ #3235

Closed

Description

@TWiStErRob

Steps to reproduce

class Tests {
	@BeforeEach 
	fun setUp(info: TestInfo) {
		System.out.println(info.displayName)
	}

	@MethodSource("methodSource")
	@ParameterizedTest(name = "{displayName} - {0}") // BUG
	fun `displayName'Zero`(param: String) {
		// displayNameZero(String) - {0}
		// displayNameZero(String) - {0}
	}

	@MethodSource("methodSource")
	@ParameterizedTest(name = "{0} - {displayName}") 
	fun `zero'DisplayName`(param: String) {
		// foo - zeroDisplayName(String)
		// bar - zeroDisplayName(String)
	}

	@MethodSource("methodSource")
	@ParameterizedTest(name = "{displayName} - {0}") 
	fun displayNameZero(param: String) {
		// displayNameZero(String) - foo
		// displayNameZero(String) - bar
	}

	@MethodSource("methodSource")
	@ParameterizedTest(name = "{0} - {displayName}") 
	fun zeroDisplayName(param: String) {
		// foo - zeroDisplayName(String)
		// bar - zeroDisplayName(String)
	}

	companion object {

		@JvmStatic
		private fun methodSource(): Array<String> =
			arrayOf(
				"foo",
				"bar"
			)
	}
}
  1. Clone https://github.com/TWiStErRob/repros/tree/main/junit/jupiter-params-displayName-vs-apostrophe
  2. gradlew test
  3. Review report at build/reports/tests/test/index.html

Note: the repro project is a bit more complex than the above:

  • TestK is a pure Kotlin version
  • TestJ is a pure Java version
  • Test is the problematic version with Kotlin apostrophes.
    I made them really similar so it's easy to compare their outputs.

Expected

No {0} anywhere.
It works correctly without apostrophes:
build/reports/tests/test/classes/com.example.MyTestK.html

Actual

When the method name contains ', the {0} is not substituted.
It fails formatting the name when apostrophes are added:
build/reports/tests/test/classes/com.example.MyTest.html

Context

  • Used versions (Jupiter/Vintage/Platform): 5.9.2
  • Build Tool/IDE: Gradle / IDEA, but likely irrelevant.

Deliverables

I think the problem might be around ParameterizedTestNameFormatter, but didn't debug it.

Activity

  1. sbrannen commented on Apr 7, 2023

    @sbrannen
    Member

    JUnit uses java.text.MessageFormat to process display names for parameterized tests.

    Here's an excerpt from the User Guide:

    Please note that name is a MessageFormat pattern. Thus, a single quote (') needs to be represented as a doubled single quote ('') in order to be displayed.

    That means if you renamed your test method to displayName''Zero, it should work.

    I say "should", because I have not tested it myself (since I don't use Kotlin).

    Please let us know if that works for you.

  2. TWiStErRob commented on Apr 7, 2023

    @TWiStErRob
    ContributorAuthor

    Yep that works as expected, but imagine test names like fun `foo shouldn''t bar`(baz: Qux) 🀒. Of course in case of contractions it's easy to expand to "should not", but there are cases when you don't have an option, like foo 'bar' baz, although that might "just work" because the quotation is closed.

    Could the name be pre-escaped somehow for MessageFormat, since using that replacement tool is just an implementation detail?

    in order to be displayed.

    I think that part of the user guide should be more explicit that if you have a ' in display name, things will break other than "not showing".

  3. 25 remaining items

  4. alemendoza-v commented on Apr 20, 2023

    @alemendoza-v
    Contributor

    Should I crΓ©ate a PR for it?

  5. sbrannen commented on Apr 21, 2023

    @sbrannen
    Member

    @alemendoza-v,

    I assumed that {displayName} would temporarily have to be replaced with something that doesn't contain curly brackets. So, yes, that appears to be the correct approach.

    Feel free to submit a PR.

    If you would like to get it included in JUnit 5.10, please do so in the coming days or week since we are now finalizing the feature set for 5.10.

    In your PR, please include Java-based tests in addition to Kotlin-based tests.

  6. changed the title [-]Apostrophe in Kotlin function name breaks ParameterizedTest(name)[/-] [+]Apostrophe in display name breaks name generation for `@ParameterizedTest`[/+] on Apr 21, 2023
  7. modified the milestones: 5.10.0-M1, 5.9.3 on Apr 24, 2023
  8. self-assigned this
    on Apr 24, 2023
  9. added a commit that references this issue on Jan 27, 2024
    7b2688c
  10. locked as resolved and limited conversation to collaborators on May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions