Android Unit Testing: Cucumber-jvm + Android Instrumentation

血红的双手。 提交于 2019-12-03 09:12:28

Your runner should inherit from Android JUnitRunner:

public class Instrumentation extends AndroidJUnitRunner {

private final CucumberInstrumentationCore instrumentationCore = new CucumberInstrumentationCore(this);

@Override
public void onCreate(final Bundle bundle) {
    instrumentationCore.create(bundle);
    super.onCreate(bundle);
}

@Override
public void onStart() {
    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:

public class BaseStepDefinitions {
public static final String TAG = BaseStepDefinitions.class.getSimpleName();

@Rule
public ActivityTestRule<StartupActivity> mActivityRule = new ActivityTestRule<>(StartupActivity.class);


@Before
public void setUp() throws Exception {
    mActivityRule.launchActivity(null);
    mActivityRule.getActivity();
}

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

}

@When("^I login with \"([^\"]*)\" and \"([^\"]*)\"$")
public void i_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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!