Reputation: 518
I am trying to automate the onboarding process of the app and need to clear app data before each @Test
I have implemented
public class Onboarding {
@Rule
public ActivityTestRule<AppStartActivity> mActivityTestRule = new ActivityTestRule<>(AppStartActivity.class);
@Before
public void clearPreferences() {
try {
// clearing app data
Runtime runtime = Runtime.getRuntime();
runtime.exec("pm clear packageName");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void Mobile10DigitWithInvalidOtp () {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
OnboardingFragment onboardingFragment = new OnboardingFragment();
onboardingFragment.LoginAsExistingUserIndia("1325546852", "12345");
onboardingFragment.invalidOtpMessage.check(matches(isDisplayed()));
}
}
But once this runs the test crashes.
Test failed to run to completion. Reason: 'Instrumentation run failed due to 'Process crashed.''. Check device logcat for details Test running failed: Instrumentation run failed due to 'Process crashed.'
Also if I run the @Test without @Before it runs fine.
How should I implement this so that I can continue running my test cases after clearing app data before each test run?
Upvotes: 4
Views: 1600
Reputation: 1995
You can use Android Test Orchestrator.
https://developer.android.com/training/testing/junit-runner#using-android-test-orchestrator
To enable Android Test Orchestrator using the Gradle command-line tool, add the following statements to your project's build.gradle file:
android {
defaultConfig {
...
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
// The following argument makes the Android Test Orchestrator run its
// "pm clear" command after each test invocation. This command ensures
// that the app's state is completely cleared between tests.
testInstrumentationRunnerArguments clearPackageData: 'true'
}
testOptions {
execution 'ANDROIDX_TEST_ORCHESTRATOR'
}
}
dependencies {
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestUtil 'androidx.test:orchestrator:1.1.0'
}
Upvotes: 0
Reputation: 6910
When you run instrumentation tests - they are running together with the application under tests in the same thread. Clearing the package or in other words killing the application under test thread will kill instrumentation tests as well.
Possible solutions:
adb
command:
adb shell pm clear com.bla.bla
@Before
test method.Upvotes: 1