Android 单元测试:Cucumber-jvm + Android 检测

Android Unit Testing: Cucumber-jvm + Android Instrumentation

使用:Cucumber-JVM with Android Instrumentation + Espresso)。

参考Githublinkhttps://github.com/mfellner/cucumber-android为此。简单的示例工作正常。

cucumber-jvm + android 检测有问题: 但在 link 中的示例中,它使用了已弃用的 ActivityInstrumentationTestCase2。我想使用@Rule - ActivityTestRule class 正如 Google.

所说

我的问题是: 对于使用 cucumber-jvm,我使用的是 CucumberInstrumentationCore 而不是 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner".

所以 Android 像 ActivityTestRule 的 @Rule 这样的 junit 注释不会被 CucumberInstrumentation 解析。那么有没有可能克服这个问题呢?

然后我决定使用 cucumber-jvm + android 仪器必须恢复。我的问题不仅针对已弃用的 class,而且在全球范围内使用 cucumber-jvm + android 检测是个好主意,因为由于注释解析,它无法使用检测功能。

你的跑步者应该继承自 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();
}

注意superclass在onCreate结束时被初始化了

然后,在 build.grade 文件中编辑 defaultConfig:

defaultConfig {
    applicationId "your.package.name"

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

最后,从 ActivityInstrumentationTestCase2 继承的步骤定义 class 应该如下所示:

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...
}

setUp 函数在每个场景之前运行,并启动 activity。

在全球范围内,如果它满足您的需求,我认为这样使用它不会有任何问题,Cucumber 注释和 JUnit 注释都可以用这种方式解析。

我创建了一个示例项目:github.com/Clutcha/EspressoCucumber