机器人馆。在测试套件中,每个下一个测试都会受到前一个测试的影响
我有多个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
或者只需添加
solo.finishOpenedActivities();
Or just add
solo.finishOpenedActivities();
不太确定您的测试套件的性质,但我在运行多个“全新开始”测试并挂在第二个测试上时遇到问题。我的问题与生成的活动有关,并通过使用 FLAG_ACTIVITY_CLEAR_TOP 启动活动来解决 - 当然这会清除堆栈,但我认为这就是您想要的?
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?
为什么不根据您正在测试的特定应用程序添加“杀死”应用程序的临时方法?
例如,根据您的应用程序活动深度,“按回 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.问题的原因是:
Robotium 使用解决方法,但它会在 ActivityInstrumentationTestCase2 测试用例启动其活动后设置其 ActivityMonitor,即:
如果您的待测活动是转发活动,则它可能会启动目标活动在 Solo 注册其 ActivityMonitor 之前。 Solo.finishOPenedActivities() 依赖于从 ActivityMonitor 收集的列表。
根据@Guillaume答案,我从测试用例或tearDown()调用此方法:
The causes of the problem are:
Robotium uses the workaround, but it sets up its ActivityMonitor AFTER your ActivityInstrumentationTestCase2 test case starts its activity, i.e.:
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():
如果您使用 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
你可以尝试删除 super.tearDown();
You can try to delete super.tearDown();
我的解决方案:
My solution: