sergio-sastre / ComposablePreviewScanner

A library to help auto-generate screenshot tests from Composable Previews with any screenshot testing library: JVM-based (i.e. Paparazzi, Roborazzi) as well as Instrumentation-based (i.e. Shot, Dropshots, Android-Testify, etc.)
MIT License
121 stars 1 forks source link

Optimization suggestion for reducing test initialization time with ParameterizedRobolectricTestRunner #13

Closed takahirom closed 2 weeks ago

takahirom commented 1 month ago

Thanks as always! I've been using the ComposablePreviewScanner library and noticed that each test takes approximately 1-5 seconds, which significantly increases the total test duration. It seems that ParameterizedRobolectricTestRunner.Parameters is invoked for each test. I found that using a lazy initialization for the previews can optimize performance. Here's a snippet that could potentially be included in the README:

companion object {
  private val previewsCache: List<ComposablePreview<AndroidPreviewInfo>> by lazy {
    val (value, time) = measureTimedValue {
      AndroidComposablePreviewScanner()
        .scanPackageTrees("your.package")
        .getPreviews()
    }
    println("Compose Preview Scanning took $time")
    value
  }

  @JvmStatic
  @ParameterizedRobolectricTestRunner.Parameters
  fun values(): List<ComposablePreview<AndroidPreviewInfo>> {
    return previewsCache
  }
}

Would it be possible to consider this approach for inclusion in the documentation to help others improve the performance of their tests?

sergio-sastre commented 1 month ago

Definitely! Thanks for taking the time to investigate the cause of the issue 🙏

sergio-sastre commented 2 weeks ago

Added to the docu. Thanks!