google / TestParameterInjector

A simple yet powerful parameterized test runner for Java.
Apache License 2.0
399 stars 34 forks source link

How to set custom name for `@TestParameter` #31

Closed hellosagar closed 1 year ago

hellosagar commented 1 year ago

TestParameterInjector will pick the test name from the parameter values using toString().

For my test case

@Test
  fun test1(
    @TestParameter("10% off discounts", "You’re off to see the world!") title: String,
    @TestParameter(
      "You unlocked 10% off your next X bookings. Your discount will be automatically " +
      "applied at checkout.",
      "Get 10% off your next booking."
    ) description: String,
    @TestParameter("Use by Dec 31, 2022", "null") expirationDate: String?,
    @TestParameter("Active", "null") status: String?,
    @TestParameter("Learn more", "null") ctaText: String?,
    @TestParameter canLearnMore: Boolean,
  ) = paparazzi.lazyViewItemSnapshot(
    Item(
      title = UIString(title),
      description = UIString(description),
      stamps = listOf(R.drawable.ic_checkmark, R.drawable.ic_confetti, R.drawable.ic_confetti),
      expirationDate = expirationDate?.let { UIString(it) },
      status = status?.let { UIString(it) },
      canLearnMore = canLearnMore,
      ctaText = ctaText?.let { UIString(it) }
    ),
    isDarkTheme = config.deviceConfig.nightMode == NightMode.NIGHT
  )

So, for above test case, test name generating is to way too long i.e screenshots_SampleScreenshotTest_test1[NEXUS_5_DAY,You’re off to see the world!,You unlocked 10% off your next X bookings. Your discount will be automatically applied at checkout.,Use by Dec 31, 2022,null,Learn more,false].

Im writing a snapshot tests using paparazzi and it uses the test name to generate the file name, due to long file name which is taken from the test name Im getting this error File name too long, and in order to solve this Ive to shorten the test name.

So, here if I want to generate a custom name for @TestParameter annotation how can I do that? In doc I found custom name is available for @TestParameters but not for TestParameter

Please suggestion some solution Thanks

hellosagar commented 1 year ago

Currently Im using a enum to avoid this situation

like below or do we have a better solution as well?

enum class Description(value: String){
    SHORT("You unlocked 10% off your next X bookings. Your discount will be automatically " +
      "applied at checkout."),
    LONG("Get 10% off your next booking.")
  }
nymanjens commented 1 year ago

For @TestParameters, there are indeed multiple solutions for this, but for @TestParamter, I think the enum option is the best way to customize the test name.