Root cause analysis of a flaky test and its resolution

Satyajit Malugu
Oct 23, 2018 · 3 min read

In the past few weeks I have been on a mission to get 💚 master build. We run the whole suite of ~270 espresso tests and they have been flaky for a looong time. This is the dirty secret of many a test automation engineers including me. The flakiness of these tests is elusive and no test fails the second time at the exact same spot, especially when you debug them locally.

So I started pruning away tests that I am know to be flaky but there is one test which was more flaky than others. While I could have the easy route of removing the test from the suite, this time I searched deep for the resolution. And I am so glad I did.


There is an adage to treat test automation code as production code. Then how come we are so accustomed to removing a flaky test over figuring out the root cause?

I am guilty of this many times. If 1/3 of our users are facing a crash sometimes, won’t we get to the bottom of it?

Overview of the problem

This particular test creates a contact with image and then verifies that our app renders this image correctly. So test code goes something like this

public class ContactsWithPhotoThreadViewTest {

public static void setUpPhoneContacts() {
Drawable testContactPhoto1 = getInstrumentation().getTargetContext().getDrawable(R.drawable.gdv_pic);
ContactsTestHelpers.createTestContactWithDrawable(testMockName1, new String[]{testMockNumber1}, testContactPhoto1);
Drawable testContactPhoto2 = getInstrumentation().getTargetContext().getDrawable(R.drawable.gdv_contact_pic);
ContactsTestHelpers.createTestContactWithDrawable(testMockName2, new String[] {testMockNumber21, testMockNumber22}, testContactPhoto2);

public static void clearPhoneContacts() {

Picture appears if the thread is a contact and the contact has a picture
public void testContactPictureAppears() {
TimelineSummaryScreenItem summaryItem = new TimelineSummaryScreenItem(threadsArea.getSummaryList(), 0);

Setup -> Create contacts

Act -> open timeline

Assert -> Check contacts has image

Data points collected in loose sequential order

  1. Also on the test lab it fails occasionally about 1 of every 3 times.
  2. There are no infrastructure failures and video and logcat seems alright, contacts are being inserted and I can see the logcat output
  3. Found an IOexception in logcat during loading of the contact image, this never showed up locally and shows consistenly everytime this test fails in FTL
  4. Found that -1 is being used for contactID instead of the real one. This is the initialization value.
  5. There are no code paths which would let contactID have -1 value.
  6. Found that there is another huge stacktrace in all these failing tests logcat

Workarounds pursued

  1. Added a pre-step to assert that no exception has been thrown when creating a contact but strangely that didn’t work too.
  2. Tried running on API level 25 and 27 instead of 26 with no avail.

Final solution

This is not a crash but a huge long stacktrace with origination point on a base view in our app.

From that stackoverflow this error sometimes happens on devices lower than API version 28 because of androidx. And we just migrated to androidx at about the same time this test started to flake out.

Also my local emulators are all version 28 so they don’t have a chance to reproduce this error at all.

So I changed our automation to run on only devices 28 and voila this flaky test isn’t flaky anymore.


While this particular test is Android, Java, Espresso stack the same principles can be applied to any stack. This post is my way of forcing myself to look more deeper into the root cause rather take the easy way out and remove the test causing a drop in test coverage and loosing the benefits of test automation.


Posts about mobile automation and testing

Satyajit Malugu

Written by


Posts about mobile automation and testing