Skip to content Skip to sidebar Skip to footer

Android Unit Testing: Cucumber-jvm + Android Instrumentation

Using: Cucumber-JVM with Android Instrumentation + Espresso). Reference Github link: https://github.com/mfellner/cucumber-android for this. The simple sample works fine. Problem w

Solution 1:

Your runner should inherit from Android JUnitRunner:

publicclassInstrumentationextendsAndroidJUnitRunner {

privatefinalCucumberInstrumentationCoreinstrumentationCore=newCucumberInstrumentationCore(this);

@OverridepublicvoidonCreate(final Bundle bundle) {
    instrumentationCore.create(bundle);
    super.onCreate(bundle);
}

@OverridepublicvoidonStart() {
    waitForIdleSync();
    instrumentationCore.start();
}

Pay attention to the super class been initialized at the end of onCreate.

Then, edit your defaultConfig in your build.grade file:

defaultConfig {
    applicationId "your.package.name"

    testApplicationId "your.steps.package"
    testInstrumentationRunner "your.package.Instrumentation"     
}

And finally, the steps definition class, which inherited from ActivityInstrumentationTestCase2 should look like:

publicclassBaseStepDefinitions {
publicstaticfinalStringTAG= BaseStepDefinitions.class.getSimpleName();

@Rulepublic ActivityTestRule<StartupActivity> mActivityRule = newActivityTestRule<>(StartupActivity.class);


@BeforepublicvoidsetUp()throws Exception {
    mActivityRule.launchActivity(null);
    mActivityRule.getActivity();
}

/**
 * All the clean up of application's data and state after each scenario must happen here
 */@AfterpublicvoidtearDown()throws Exception {

}

@When("^I login with \"([^\"]*)\" and \"([^\"]*)\"$")publicvoidi_login_with_and(String user, String password)throws Throwable {
   // Login...
}

The setUp function runs before each scenario, and launching the activity.

Globally, if it serves your needs I don't see any problem using it like so, both Cucumber annotations and the JUnit annotations can be parsed in this way.

I've created a sample project: github.com/Clutcha/EspressoCucumber

Post a Comment for "Android Unit Testing: Cucumber-jvm + Android Instrumentation"