gpt4 book ai didi

android - Espresso如何测试Activity是否完成?

转载 作者:IT老高 更新时间:2023-10-28 22:20:30 24 4
gpt4 key购买 nike

我想断言,当执行某些操作时,我当前正在测试的 Acitivty 已完成。不幸的是,到目前为止,我只是通过在测试结束时添加一些 sleep 来断言它。有没有更好的办法?

import android.content.Context;
import android.os.Build;
import android.support.test.rule.ActivityTestRule;
import android.test.suitebuilder.annotation.LargeTest;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import static org.junit.Assert.assertTrue;

@SuppressWarnings("unchecked")
@RunWith(JUnit4.class)
@LargeTest
public class MyActivityTest {

Context context;

@Rule
public ActivityTestRule<MyActivity> activityRule
= new ActivityTestRule(MyActivity.class, true, false);

@Before
public void setup() {
super.setup();
// ...
}

@Test
public void finishAfterSomethingIsPerformed() throws Exception {

activityRule.launchActivity(MyActivity.createIntent(context));

doSomeTesting();

activityRule.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
fireEventThatResultsInTheActivityToFinishItself();
}
});

Thread.sleep(2000); // this is needed :(

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
assertTrue(activityRule.getActivity().isDestroyed());
}

}

}

最佳答案

就我而言,我可以测试 isFinishing():

assertTrue(activityTestRule.getActivity().isFinishing());

代替:

    Thread.sleep(2000); // this is needed :(

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
assertTrue(activityRule.getActivity().isDestroyed());
}

isFinishing() 的另一个优点是,您不需要版本检查。

关于android - Espresso如何测试Activity是否完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35863134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com