使用Cucmber和RxJava在AndroidTest中进行活动清理



我在黄瓜测试结束时观察到一个奇怪的堆栈跟踪。 我的测试基于Cucumber-jvm android示例。我使用 RxJava2 在@Then方法中执行了一些异步过程。没有异步过程,一切都很好。

测试是成功的,但在@After黄瓜方法期间发生了一些事情。跑步者似乎没有等到正确完成活动。我尝试使用规则 finishActivity 方法或在完成方法之后放置一个 waitForIdleSync(( 方法,但这些都不起作用。 如果我放一个 Thread.sleep(1000(,它可以完成这项工作,但我想避免在我的测试类中依赖时间常量。

有人遇到同样的问题吗?并罚款解决方案?

感谢您的建议。

我的步骤伪代码:

公共类活动步骤 {

@Rule
ActivityTestRule<EmptyActivity> rule = new ActivityTestRule<>(EmptyActivity.class, false, false);

@Before
public void launchActivity() throws Exception {
EmptyActivity appFakeActivity = rule.launchActivity(null);
}
@After
public void finishActivity() throws Exception {
getActivity().finish();
// TODO : Inspect stackTrace at the end of the final executed test. It seems that the test runner close before the activity is effectively stopped. 
}
private Activity getActivity() {
return rule.getActivity();
}
@Given("...")
public void given() {
}
@When("...")
public void when_step() {
...
}
@Then("...")
public void valid_M1() {
Single<String> single = Single.just("qsdf");
TestObserver<String> observer = new TestObserver<>();
single
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
observer.awaitTerminalEvent();
}

并且运行器关闭时的堆栈跟踪是:

I/MonitoringInstr: Unstopped activity count: 1
I/MonitoringInstr: Unstopped activity count: 1
I/MonitoringInstr: Unstopped activity count: 1
I/MonitoringInstr: Unstopped activity count: 1
E/THREAD_STATE:   Thread[FinalizerWatchdogDaemon,5,system]
java.lang.Thread.sleep(Native Method)
java.lang.Thread.sleep(Thread.java:1031)
java.lang.Thread.sleep(Thread.java:985)
java.lang.Daemons$FinalizerWatchdogDaemon.sleepFor(Daemons.java:257)
java.lang.Daemons$FinalizerWatchdogDaemon.waitForFinalization(Daemons.java:268)
java.lang.Daemons$FinalizerWatchdogDaemon.run(Daemons.java:216)
java.lang.Thread.run(Thread.java:818)
Thread[Instr: com.schneider.electric.forum.integration.m1.test.CucumberRunner,5,main]
dalvik.system.VMStack.getThreadStackTrace(Native Method)
java.lang.Thread.getStackTrace(Thread.java:580)
java.lang.Thread.getAllStackTraces(Thread.java:522)
android.support.test.runner.MonitoringInstrumentation.getThreadState(MonitoringInstrumentation.java:636)
android.support.test.runner.MonitoringInstrumentation.dumpThreadStateToOutputs(MonitoringInstrumentation.java:631)
android.support.test.runner.MonitoringInstrumentation.waitForActivitiesToComplete(MonitoringInstrumentation.java:384)
android.support.test.runner.MonitoringInstrumentation.finish(MonitoringInstrumentation.java:347)
cucumber.api.android.CucumberInstrumentationCore.start(CucumberInstrumentationCore.java:92)
com.schneider.electric.forum.integration.m1.test.CucumberRunner.onStart(CucumberRunner.java:44)
android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837)
Thread[FinalizerDaemon,5,system]
java.lang.Object.wait(Native Method)
java.lang.Object.wait(Object.java:422)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:101)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:72)
java.lang.Daemons$FinalizerDaemon.run(Daemons.java:173)
java.lang.Thread.run(Thread.java:818)
Thread[JDWP,5,system]
Thread[Binder_2,5,main]
...
W/MonitoringInstr: Still 1 activities active after waiting 2000 ms.
I/MonitoringInstr: waitForActivitiesToComplete() took: 2042msTests ran to completion.

这可能有助于提供一些解释:

https://github.com/android/android-test/issues/211

但是似乎没有其他建议的解决方案,除了添加定时等待Thread.sleep(),正如您已经尝试过的那样。这应确保活动有机会完成。

或者,您可以尝试从ActivityTestRule迁移到ActivityScenario

相关内容

  • 没有找到相关文章

最新更新