AndroidJUnit4 测试:保持 activity 存活
AndroidJUnit4 Test: Keep activity alive
在 AndroidJUnit4 完成某个测试后,我试图让我的 activity 存活下来。我想这样做的原因是有时我只是想看看 UI 的样子以及触觉的感觉。出于这样的目的,我不想构建整个应用程序并导航到所需的站点。
我读到,可以覆盖 tearDown() 以防止关闭 activity。
虽然 InstrumentationTestCase is deprecated and Android suggests to use the InstrumentationRegistry and that new tests should be written with the new Testing Support Library.
如何使用这个新库在测试完成后保持 activity 存活?
我找到了适合我的解决方案。如果您有更好的建议,请告诉我。
AndroidJUnit4 确实有 tearDown 和 setUp,但它不是覆盖。必须使用注解来实现:
@Before
public void setUp() throws Exception {
//do something
}
@After
public void tearDown() throws Exception {
//do something
}
为了在测试通过后保持activity存活,我让线程在tearDown方法中等待:
@After
public void tearDown() throws Exception {
synchronized (this) {
wait();
}
}
如果你想在特定时间后唤醒线程(但我建议在这种情况下使用 Thread.sleep() )或者如果你想触发唤醒呼叫,你可以轻松组合带锁的同步块。
在 AndroidJUnit4 完成某个测试后,我试图让我的 activity 存活下来。我想这样做的原因是有时我只是想看看 UI 的样子以及触觉的感觉。出于这样的目的,我不想构建整个应用程序并导航到所需的站点。
我读到,可以覆盖 tearDown() 以防止关闭 activity。
虽然 InstrumentationTestCase is deprecated and Android suggests to use the InstrumentationRegistry and that new tests should be written with the new Testing Support Library.
如何使用这个新库在测试完成后保持 activity 存活?
我找到了适合我的解决方案。如果您有更好的建议,请告诉我。
AndroidJUnit4 确实有 tearDown 和 setUp,但它不是覆盖。必须使用注解来实现:
@Before
public void setUp() throws Exception {
//do something
}
@After
public void tearDown() throws Exception {
//do something
}
为了在测试通过后保持activity存活,我让线程在tearDown方法中等待:
@After
public void tearDown() throws Exception {
synchronized (this) {
wait();
}
}
如果你想在特定时间后唤醒线程(但我建议在这种情况下使用 Thread.sleep() )或者如果你想触发唤醒呼叫,你可以轻松组合带锁的同步块。