Android单元测试:Cucumber-jvm + Android Instrumentation [英] Android Unit Testing: Cucumber-jvm + Android Instrumentation
问题描述
使用:带有Android Instrumentation + Espresso的Cucumber-JVM).
参考Github链接: https://github.com/mfellner/cucumber -android .简单的示例工作正常.
Reference Github link: https://github.com/mfellner/cucumber-android for this. The simple sample works fine.
cucumber-jvm + android工具的问题: 但是在链接的示例中,它使用了不推荐使用的ActivityInstrumentationTestCase2.我想使用Google所说的@Rule-ActivityTestRule类.
Problem with cucumber-jvm + android instrumentation: But in the sample in link, it uses ActivityInstrumentationTestCase2 which is deprecated. I would like to use @Rule - ActivityTestRule class as said by Google.
这是我的问题: 对于使用cumulage-jvm,我使用的是CucumberInstrumentationCore而不是 testInstrumentationRunner"android.support.test.runner.AndroidJUnitRunner".
Here my question is: For using cucumber-jvm, I am using the CucumberInstrumentationCore instead of testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner".
因此CucumberInstrumentation不会解析Android junit注释,例如@Rule for ActivityTestRule.那么有可能克服这个问题吗?
So Android junit annotations like @Rule for ActivityTestRule is not parsed by CucumberInstrumentation. So Is it possible to overcome this problem?
然后,我决定恢复使用cumul-jvm + android工具.我的问题不仅是针对已弃用的类,而且在全球范围内,最好还是使用cumming-jvm + android工具,因为注释解析无法使用工具功能.
Then is my decision to use cucumber-jvm + android instrumentation has to be reverted back. My question is not only for the deprecated class but globally is it good idea to go for cucumber-jvm + android instrumentation, as it can't use instrumentation features because of annotation parsing.
推荐答案
您的跑步者应继承自Android JUnitRunner:
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();
}
请注意,超级类已在onCreate的结尾处初始化.
Pay attention to the super class been initialized at the end of onCreate.
然后,在build.grade文件中编辑defaultConfig:
Then, edit your defaultConfig in your build.grade file:
defaultConfig {
applicationId "your.package.name"
testApplicationId "your.steps.package"
testInstrumentationRunner "your.package.Instrumentation"
}
最后,继承自ActivityInstrumentationTestCase2的步骤定义类应类似于:
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...
}
setUp函数在每种情况下运行,然后启动活动.
The setUp function runs before each scenario, and launching the activity.
在全球范围内,如果可以满足您的需求,那么使用它不会出现任何问题,可以用这种方式解析Cucumber批注和JUnit批注.
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.
我创建了一个示例项目: github.com/Clutcha/EspressoCucumber
I've created a sample project: github.com/Clutcha/EspressoCucumber
这篇关于Android单元测试:Cucumber-jvm + Android Instrumentation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!