11

I setup Espresso instrumentation framework to run my Android Functional Automation tests. For every test, I want to login to the app and delete the app after I finish the test.

So, I setup something like below:

public class FirstSampleTest extends BaseTest {

private final BaseTest baseTest;

// private final ElementUtils elementUtils;

public FirstSampleTest() throws InterruptedException {
    this.baseTest = new BaseTest();
}

@Before
public void initiate() throws InterruptedException {
    //I have setup login method here to login to the app after it installs
}

@Rule
public ActivityTestRule<SplashScreenActivity> splashScreenActivityActivityTestRule = new ActivityTestRule(SplashScreenActivity.class);

@Test
public void testTheHomeScreen() throws InterruptedException {
   //Some tests go here. 
}

@After
public void teardown() throws InterruptedException {
    //I want to uninstall the app or delete it from the emulator once the test is run 
}

}
user5599934
  • 311
  • 5
  • 13

2 Answers2

9

You can add a gradle task in the Android Studio Before launch section in Run -> Edit Configurations.

Click + -> Add a gradle-aware Make -> :app:uninstallAll

note: "app" in :app:uninstallAll depends on your main module name. So it can be :my_module:uninstallAll, or :company:uninstallAll

Allen
  • 2,979
  • 1
  • 29
  • 34
  • Be careful if you have an unrelated, debuggable Android device plugged in while you're doing this. It seems that `:app:uninstallAll` uninstalls your app (the app associated with the currently open project) on _all_ available devices. – greeble31 May 10 '20 at 18:08
3

Uninstalling the app from the Instrumentation tests is not possible. However, once all the tests are run, the app is uninstalled automatically.

Note: The app is not uninstalled only when a single test is run. Please run the whole build using the command ./gradlew connectedAndroidTest

jellyBeans
  • 293
  • 2
  • 4
  • 12
user5599934
  • 311
  • 5
  • 13
  • I think that user5599934 is talking about the test itself being removed and not your actual app. I think your question is how to remove that actual app between tests which is something I want to to do be able to test my new API 24 permission code. Here is one way http://stackoverflow.com/questions/20705711/uninstall-app-before-start-test – Monte Creasor Jun 24 '16 at 02:05
  • how do I get the test to STOP uninstalling the app?? – Phlip Aug 17 '23 at 22:29