机器人馆。在测试套件中,每个下一个测试都会受到前一个测试的影响

发布于 2024-12-11 08:43:07 字数 390 浏览 1 评论 0原文

我有多个 UI 测试。当我运行单个测试时,一切正常。但是如果我运行一批(作为 CI 构建的一部分)测试就会失败,因为首先进行的测试会更改应用程序的状态,而接下来的测试会受到这些更改的影响。 (因为应用程序没有被杀死)。

我在 tearDown() 中尝试了 getActivity().finish()
尝试了 solo.finalize() 它实际上做了同样的事情。

有没有办法在每次测试运行开始时拥有一个新的应用程序? (使用 Robotium)。
有没有办法在测试结束时以编程方式终止应用程序?
我将 ActivityInstrumentationTestCase2 与 Robotium 一起使用

I have multiple UI tests. When I run a single test, everything is OK. But if I run a batch of them (as a part of CI build) test fail, because tests that go first change the state of the application, and the next tests are affected by those changes. (Since the app is not getting killed).

I tried getActivity().finish() in tearDown().
Tried solo.finalize() which does the same actually.

Is there a way to have a fresh app at the beginning of each test run? (Using Robotium).
And is there a way to programmatically kill the app at the end of a test?
I'm using ActivityInstrumentationTestCase2 with Robotium

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

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

发布评论

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

评论(7

动听の歌 2024-12-18 08:43:07

或者只需添加 solo.finishOpenedActivities();

Or just add solo.finishOpenedActivities();

浅紫色的梦幻 2024-12-18 08:43:07

不太确定您的测试套件的性质,但我在运行多个“全新开始”测试并挂在第二个测试上时遇到问题。我的问题与生成的活动有关,并通过使用 FLAG_ACTIVITY_CLEAR_TOP 启动活动来解决 - 当然这会清除堆栈,但我认为这就是您想要的?

    Intent i = new Intent();
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    setActivityIntent(i);
    solo = new Solo(getInstrumentation(), getActivity());

Not exactly sure of the nature of your test suite but I had problems running multiple "fresh start" tests and hanging on second test. My problem related to spawned activities and was resovled by launching the activity with FLAG_ACTIVITY_CLEAR_TOP - of course this clears the stack but I think that's what you want?

    Intent i = new Intent();
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    setActivityIntent(i);
    solo = new Solo(getInstrumentation(), getActivity());
当爱已成负担 2024-12-18 08:43:07

为什么不根据您正在测试的特定应用程序添加“杀死”应用程序的临时方法?
例如,根据您的应用程序活动深度,“按回 3 次” 或类似的内容可能就足够了。

您可以将其添加到测试超类的 tearDown 方法中,以便它在每个测试之后运行。

您应该将您的 Robotium 测试视为用户案例、验收测试,而不是普通的单元测试(它们不是!)。因此,如果您想关闭应用程序,请在这些测试中完全按照您期望用户执行的操作来关闭应用程序。

Why not adding an adhoc way of "killing" the app, depending on the specific app you're testing?
For example, depending on your application activity depth, "press back 3 times" or something similar could be good enough.

You could add that in the tearDown method of your tests superclass, so that it's ran after each of your tests.

You should think about your Robotium tests not as normal unit-tests (they're not!), but as user-cases, acceptance-tests. So if you want to close the app, do in these tests exactly what you would expect the user to do to close the app.

后eg是否自 2024-12-18 08:43:07

问题的原因是:

  1. 没有 Android API 来获取堆栈中所有活动的列表。
  2. (1) 的解决方法是使用 ActivityMonitor 来跟踪每个启动的 Activity。
  3. Robotium 使用解决方法,但它会在 ActivityInstrumentationTestCase2 测试用例启动其活动后设置其 ActivityMonitor,即:

    活动 Activity = getActivity();
    独奏独奏 = new Solo(getInstrumentation(), 活动);
    

如果您的待测活动是转发活动,则它可能会启动目标活动在 Solo 注册其 ActivityMonitor 之前。 Solo.finishOPenedActivities() 依赖于从 ActivityMonitor 收集的列表。

根据@Guillaume答案,我从测试用例或tearDown()调用此方法:

private void backOutToHome() {
    boolean more = true;
    while(more) {
        try {
            getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
        } catch (SecurityException e) { // Done, at Home.
            more = false;
        }
    }
}

The causes of the problem are:

  1. There is no Android API to get a list of all the activities in a stack.
  2. A workaround for (1) is to use an ActivityMonitor to keep track of each Activity that starts.
  3. Robotium uses the workaround, but it sets up its ActivityMonitor AFTER your ActivityInstrumentationTestCase2 test case starts its activity, i.e.:

    Activity activity = getActivity();
    Solo solo = new Solo(getInstrumentation(), activity);
    

If your activity-under-test is a forwarding activity, then it is likely starting the destination activity before Solo registers its ActivityMonitor. Solo.finishOPenedActivities() relies on the list that it collected from its ActivityMonitor.

As per the @Guillaume answer, I call this method from the test case or from tearDown():

private void backOutToHome() {
    boolean more = true;
    while(more) {
        try {
            getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
        } catch (SecurityException e) { // Done, at Home.
            more = false;
        }
    }
}
季末如歌 2024-12-18 08:43:07

如果您使用 maven 或 ant 运行构建(Robotium 是 JUnit 测试的便捷包装器),则可以选择为每个测试类甚至测试用例创建一个新进程。这提供了干净的环境,但会减慢测试执行速度。

我个人更喜欢坚持使用普通的 Junit / TestNG 并利用模拟(使用 jMockit)来确保我的代码和 android 之间的正确交互。请参阅此处的示例:

https ://github.com/ko5tik/andject/blob/master/src/test/java/de/pribluda/android/andject/ViewInjectionTest.java

If you run your build with maven or ant (Robotium is a convenience wrapper for JUnit-Tests), there is an option to fork a new process for every test class or even test case. This provides clean environment, but slows down test execution.

I personally prefer to stick with vanilla Junit / TestNG and utilize mocking (with jMockit) to assure proper interaction beween my code and android. See sample here:

https://github.com/ko5tik/andject/blob/master/src/test/java/de/pribluda/android/andject/ViewInjectionTest.java

暮年慕年 2024-12-18 08:43:07

你可以尝试删除 super.tearDown();

You can try to delete super.tearDown();

哥,最终变帅啦 2024-12-18 08:43:07

我的解决方案:

    @Override
    public void tearDown() throws Exception {

        solo.finishOpenedActivities();

        super.tearDown();
    }

My solution:

    @Override
    public void tearDown() throws Exception {

        solo.finishOpenedActivities();

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