[英]Android test annotations with Robotium
我目前正在Android上构建一个应用程序,并使用Robotium进行功能测试(顺便说一句,不要在Android 1.6或更低版本的机器上使用Robotium,这太容易出错了)。
这些测试中的某些测试有随机失败的趋势,主要是Robotium缺少文本字段,或者超时而不读取文本。 我正在尝试使用@FlakyTest
批注,因此它们将运行两次或三次,然后抛出失败的测试错误。 但是,注释不起作用,失败后不会重新运行测试。
这是我使用注释的方式:
public class ClassName extends ActivityInstrumentationTestCase2<HomeActivity>{
@LargeTest
@FlakyTest(tolerance=3)
public void testMethod(){
//Here I run my roboitium scripts.
}
}
然后我从命令行运行它:
亚行工具外壳-w com.jayway.test / android.test.InstrumentationTestRunner
eclipse或测试的命令行执行都不会考虑不稳定的测试注释。 有人看到我尝试应用@FlakyTest
吗?
通常,在为Android(带有或不带有Robotium)编写测试时,您必须更加小心。 您不能只说“这是可见的”。 您需要将所有内容包装在“等待”周期中,因此会说“等待此可见”。 当在仿真器中运行时,这尤其是个问题,因为有时事情会花很长时间而没有任何正当的理由。 没有等待周期,您将永远不会获得一致的运行。 我们有数百个测试,而且我们从来不需要使用FlakyTest批注。
我看不到您使用@FlakyTest
批注的任何问题。
我整理了一个快速测试案例来测试@FlakyTest和Robotium(v2.2):
public class FlakyTestCase extends ActivityInstrumentationTestCase2<Main> {
private static int count = 0;
private Solo solo;
public FlakyTestCase() {
super("com.stackoverflow.example", Main.class);
}
@Override
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
@LargeTest
@FlakyTest(tolerance=3)
public void testFlaky(){
Log.e("FlakeyTestCase", "Execution Count:" + ++count);
solo.assertCurrentActivity(null,Main.class);
solo.clickOnText("Doesn't Exist");
Log.e("FlakeyTestCase", "Shouldn't make it here");
}
}
LogCat显示以下消息:
Execution Count: 1
Execution Count: 2
Execution Count: 3
因此,肯定会调用@FlakyTest
批注。 测试的(最终)失败显示为:
junit.framework.AssertionFailedError: The text: Doesn't Exist is not found!
消息"Shouldn't make it here"
不做"Shouldn't make it here"
从未被记录过。
据我所知,您如何声明注释或@FlakyTest
和Robotium v2.2都没有问题。
也许测试代码的另一部分有问题?
Robotium缺少文本字段或超时,无法阅读文本,这意味着我们必须清楚地检查屏幕上是否存在文本或任何文本,然后只需执行以下操作
if(solo.searchText("Doesn't Exist", true){
solo.clickOnText("Doesn't Exist");
}
如果有任何类似按钮或其他组件的组件,我们也可以通过上述逻辑来实现。
将此添加到您的代码:
import android.util.Log;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.