robolectric / robolectric

Android Unit Testing Framework
http://robolectric.org
Other
5.91k stars 1.37k forks source link

Feature request: API to help raise test coverage for composable functions #6759

Open alwa opened 3 years ago

alwa commented 3 years ago

With Jetpack Compose, https://developer.android.com/jetpack/compose, it is difficult to produce high test coverage due to the way these components are defined; there is an exponential growth of test cases needed for each new parameter.

For example it is very common to pass an optional Modifier parameter. I would want an assertion API that verifies the behavior for a composable, kind of like https://jqno.nl/equalsverifier/

Something like:

ComposableVerifier.for(Composables::MyComposable).withParameter("modifier").verify();

which would just check that the composable doesn't crash or similar given different inputs of modifier

Any thoughts on this? Any other suggestions?

Related: https://github.com/jacoco/jacoco/issues/1208

alwa commented 3 years ago

I've reported an alternative approach to this problem. If previews were located in test files this wouldn't be a problem. Please vote here: https://issuetracker.google.com/u/1/issues/201895467

Update 2023-06-02: I find this comment confusing. It would definitely still be a problem even after removing @Preview Composables. The problem is branch coverage. In theory it would be possible to test all branches by writing loads of tests. But I would prefer a test API that handles this for me, as stated in this ticket

utzcoz commented 1 year ago

@alwa What I understand is that it needs compose to support it to let Jacoco get proper test coverage information for composable function?

alwa commented 1 year ago

@alwa What I understand is that it needs compose to support it to let Jacoco get proper test coverage information for composable function?

@utzcoz This was a while ago, but from what I can remember:

  1. Jacoco today supports Composable functions and gathers some coverage
  2. Jacoco is framework agnostic, meaning it will not do any "special" handling of Composables so we need to solve it in some other way, like injecting variants of input automatically

References supporting my two claims:

  1. Personal experience :-)
  2. https://github.com/jacoco/jacoco/pull/1259#issuecomment-1551444097
hoisie commented 1 year ago

cc @JoseAlcerreca, who is interested in improving testability of things like Compose.