Robotium自动化测试的注意项
1.基于eclipse上的Robotium自动化测试
1.AssertionFailedError异常
junit.framework.AssertionFailedError: Button with text: 'login' is not found! at com.robotium.solo.Clicker.clickOn(Clicker.java:474) at com.robotium.solo.Solo.clickOnButton(Solo.java:941) at com.example.testdemo.test.WykTest.test(WykTest.java:30) at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214) at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199) at android.test.ActivityInstrumentationTestCase2.runTest
2.该异常是由于执行到代码的 ” MyActivityInstrumentationTestCase2 “类的solo.clickOnButton(“login”)该处,由于找不到 文字”login”对应的Button 导致的异常;
3.解决: 改 ” “<”Button android:text=”* “/> “的值改为 “login”;
Solo关于clickOnButton源码的截图:
2.基于Android Studio上的Robotium自动化测试
1. hamcrest-core:jar:1.3和junit 4.12冲突
1.异常
Error:Execution failed for task ':app:transformClassesWithJarMergingForDebugAndroidTest'. com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/hamcrest/BaseDescription.class
2.看看代码中哪处地方引用到了
@Test //这里引用了 public void test(){ solo.clickOnButton("login"); }
3.尝试
1.将 “junit:4.12” 改为 junit:4.10,不起作用;
2.由于两个jar包都有 “BaseDescription”,将 ” hamcrest-core:jar:1.3 ” jar包删除,不起作用;
3.改:
public void testHy(){ //可行的 solo.clickOnButton("login"); }
4.使用 “junit:4.12” ,为何会扯到”hamcrest-core:jar:1.3”,可能是junit引用 “hamcrest”(具体不知) ,因为hamcrest可以有效增加junit的测试能力;
2.编译时异常
1.异常信息
Test running failed: Unable to find instrumentation info for: ComponentInfo{com.yhsoft.yh.wyktest.test/android.test.InstrumentationTestRunner}
2.解决方案:
module下的build.gradle文件 加: android { defaultConfig { multiDexEnabled true testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner" } }
3.NullPointerException异常
1.熟悉的老朋友NullPointerException,看报错信息
Test failed to run to completion. Reason: 'Instrumentation run failed due to 'java.lang.NullPointerException''. Check device logcat for details Test running failed: Instrumentation run failed due to 'java.lang.NullPointerException'
2.这不是扯淡,都没提醒空指针空哪里,还是看异常日志吧,对的!布局写错了!!!!!
3.总结:
-
- 不禁感叹:测试也是很深的门道!
-
- 中秋快乐! 合家团圆