被测试Project : HelloWorld Package: com.test.helloworld
1. 打开Eclipse, File->New->Project->Android Test Project, 下一步
2. New Android Test Project中,Project Name: HelloWorld Test ,下一步 , Select Test Tatget, An existing Android project中选择 HelloWorld, 完成
3. Hello World Test->src->com.test.helloworld.test, 选择new->class, Name中填入UITextTest, 完成
4. Hello World Test->Build path->Add External JARs->robotium.jar (download url:http://code.google.com/p/robotium/downloads/list )
5. 修改UITextTest.java 内容如下:
package com.test.helloworld.test;
import android.test.ActivityInstrumentationTestCase2;
import com.test.helloworld.HelloWorldActivity; //导入需要测试的工程
import com.jayway.android.robotium.solo.Solo;
// 导包,以上是测试工程需要的包
// 测试工程中的类要继承ActivityInstrumentationTestCase2类,泛型写需要测试的工程的入口activity名:HelloWorldActivity
public class UITextTest extends ActivityInstrumentationTestCase2<HelloWorldActivity>{
private Solo solo;
public UITextTest() {
super(HelloWorldActivity.class); //创建构造方法
}
// 在测试方法前覆写父类的setUp()方法,该方法用来初始化solo,绑定对应的Activity
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
// 具体实现测试用例的方法
public void testUI() throws Exception {
boolean expected =true;
boolean actual =solo.searchText("HelloWorldActivity") &&solo.searchText("Hello");
assertEquals("This and/or is are not found", expected, actual);
}
// 需要在测试方法后覆写父类的tearDown()方法,该方法用来清理资源垃圾,关闭activity
public void tearDown() throws Exception {
try {
solo.finalize();
} catch (Throwable e) {
e.printStackTrace();
}
getActivity().finish();
super.tearDown();
}
}
6. Hello World Test->Run As->Android JUnit Test
7. 若不通过,提示:
java.lang.NoClassDefFoundError: com.jayway.android.robotium.solo.Solo
at com.skymobi.android.qc.test.RobotTest.setUp(RobotTest.java:46)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
在eclipse中进行如下设置
"Properties > Java Build Path > Libraries", delete "Android Dependencies" in my test project.
"Properties > Java Build Path > Libraries", delete two robotium jar and click "Add JARs" to re-import robotium jar in my test project.
"Properties > Java Build Path > Order and Export", check two checkbok of robotium jar (and othera you want exported.)
Clean my test project.
8. Rerun Test
本文仅作整理记录,非原创。