6

Dealing with flaky tests in Jetpack Compose

 3 years ago
source link: https://proandroiddev.com/managing-flaky-tests-in-jetpack-compose-89c598590068
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Dealing with flaky tests in Jetpack Compose

Jetpack Compose is right around the corner and testing your components is a very important part of the development cycle. There are several test examples in the official Jetpack Compose Samples repository to help you out but they cover simpler scenarios.

The basic syntax for testing in Jetpack Compose is pretty straightforward. Let’s take a look in our example:

In the test above we create a test to validate if the title is displayed in our Composable. For more information about different types of tests, please access the official documentation.

However, sometimes we want to test the same Composable with different states to see how it reacts. For example, imagine we are developing a Compose that renders a list, it is a good idea to compose it with a lot of different inputs to cover different scenarios. In this case, instead of setting it on @Before, we actually init it on each test.

Now we are delegating the creation of the Composable to the initComposable function that is called on each test. Simple and clean.

When migrating my personal application Alkaa to Jetpack Compose, I faced an issue during the tests: some of my components mix Jetpack Compose and View System and due to some delay between their interaction, my tests are flaky. It means that the test “sometimes fails, but if you retry it enough times, it passes, eventually”.

Therefore for these specific tests I would use an specific rule to be more permissive with the test. The rule in question is @AllowFlaky from Barista library which repeats the test for a number of times if it fails. After this update, that was my test code:

However, for my surprise the test crashed with the message java.lang.IllegalStateException: Cannot call setContent twice per test!. Since our test function is repeated if failed in the first time, it will try to re-run but the content is already set on our test Activity and the crash occours.

Again, I asked on the kotlinlang Slack. This time

helped me out.
1*_vKACw0girG1gqJgNMDrsg.png?q=20
managing-flaky-tests-in-jetpack-compose-89c598590068
https://kotlinlang.slack.com/archives/CJLTWPH7S/p1614167416410700

The solution we found is instead of using createComposeRule, we use createEmptyComposeRule in addition to ActivityScenario to launch and close our content on demand. Let’s see how everything connects:

After updating to createEmptyComposeRule we no longer have the function setContentin our rule, now the setup happens ononActivity and the content is set inside this function. During the test setup we now launch the default ComponentActivity with no content and set it during each test. After each test we close the scenario finishing the activity and cleaning any state for the next test.

That’s it! Now we can use @Repeat or @Flaky in our tests smoothly. 🎉

If you want to dig deeper in some real scenario code, this is the commit in my personal project updating some Compose tests to be flaky:

Final thoughts

We should strive to avoid flaky tests, as their presence keeps us from having the confidence required in our applications. On the other hand, it is very difficult to avoid flakiness in some UI tests due to different device configurations.

If some handling is needed, it is nice to have alternatives and Jetpack Compose provides it integrating with the good ol’ ActivityScenario. It allows not only managing flaky tests but also having fine-grained lifecycle tests.

I hope that this article can help you better managing your Instrumented Tests and give ideas about how flexible the Jetpack Compose test library already is in its early stages.😊

Special thanks to

for the emptyComposeTestRule trick and thank you for reading my article.❤️

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK