Android 测试 - ActivityInstrumentationTestCase2 存在问题?

发布于 2024-12-21 04:29:45 字数 840 浏览 3 评论 0原文

我正在使用 Robotium 和 ActivityInstrumentationTestCase2 运行 Android 版 UIAutomation。我有一个包含 5 个测试的测试套件。 有时我的测试会随机崩溃,因为一旦前一个测试尚未结束,测试就开始了。 有办法避免这种情况吗?是否可以在每次测试之前手动添加 10 秒的延迟来避免这个可怕的烦人的错误?

编辑:

public class MyTest<T extends RoboActivity> extends ActivityInstrumentationTestCase2<T>
{

    protected Solo solo;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        solo = new Solo(getInstrumentation(), getActivity());
    }

    @Override
    protected void tearDown() throws Exception {
        solo.finishOpenedActivities();

        try {
            solo.finalize();
        }
        catch (Throwable e) {
            Assert.fail(e.getMessage()+ e.toString());
            e.printStackTrace();
        }

        super.tearDown();
    }
}

I am running UIAutomation for android using Robotium and ActivityInstrumentationTestCase2. I have a test suite with 5 tests.
Sometimes my test randomly crash because a test starts, once the previous test has not ended yet.
Is there a way to avoid this? is it possible to manually add a 10 second delay before every test to get away from this horrible annoying bug?

EDIT:

public class MyTest<T extends RoboActivity> extends ActivityInstrumentationTestCase2<T>
{

    protected Solo solo;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        solo = new Solo(getInstrumentation(), getActivity());
    }

    @Override
    protected void tearDown() throws Exception {
        solo.finishOpenedActivities();

        try {
            solo.finalize();
        }
        catch (Throwable e) {
            Assert.fail(e.getMessage()+ e.toString());
            e.printStackTrace();
        }

        super.tearDown();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

澜川若宁 2024-12-28 04:29:45

也许这可行:

mSolo = new Solo(getInstrumentation(), getActivity());
mSolo.waitForActivity(AccountDetail.class);

似乎 waitFor* 方法比“睡眠”更好地管理这一点
http://robotium.googlecode .com/svn/doc/com/robotium/solo/Solo.html#waitForActivity(java.lang.Class, int)

Maybe this could work :

mSolo = new Solo(getInstrumentation(), getActivity());
mSolo.waitForActivity(AccountDetail.class);

Seems that waitFor* methods are managing that better than a "sleep"
http://robotium.googlecode.com/svn/doc/com/robotium/solo/Solo.html#waitForActivity(java.lang.Class, int)

或十年 2024-12-28 04:29:45

我的测试的构建、拆卸等略有不同,但运行良好(见下文)。我必须添加睡眠来解决一些不确定的测试失败问题。

public class AccountDetailTest extends ActivityInstrumentationTestCase2<AccountDetail> {

private Solo solo;

public AccountDetailTest() {
    super("com.bigcorp.breadmaker", AccountDetail.class);
}

@Override
public void setUp() {
    solo = new Solo(getInstrumentation(), getActivity());
    solo.sleep(500); //wait for activity to initialize
}

@SmallTest
public void testDummy() {
    assertNotNull(solo);
}

@Override
public void tearDown() throws Exception {
    solo.finishOpenedActivities();
}
}

My tests's construction, teardown, etc. are slightly different and works well (see below). I had to add a sleep to work around some non-deterministic test-failures.

public class AccountDetailTest extends ActivityInstrumentationTestCase2<AccountDetail> {

private Solo solo;

public AccountDetailTest() {
    super("com.bigcorp.breadmaker", AccountDetail.class);
}

@Override
public void setUp() {
    solo = new Solo(getInstrumentation(), getActivity());
    solo.sleep(500); //wait for activity to initialize
}

@SmallTest
public void testDummy() {
    assertNotNull(solo);
}

@Override
public void tearDown() throws Exception {
    solo.finishOpenedActivities();
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文