本文转自:http://www.51testing.com/?uid-115892-action-viewspace-itemid-223476
1. 创建一个简单的工程(可参考本人的日志《第一个应用HelloWorld》,需要对该工程稍做修改)作为被测程序,运行后显示如下界面:
2. 创建一个Test Project
1). 打开eclipse,选择File->New->Project…->Android Test Project,点击Next。
2). 在Test Project Name中输入测试工程的名称,如:HelloWorldTest。选择An existing Android Project,点击右边的Browse…按钮。
3). 选择被测工程,如之前创建的HelloWorldnp,点击OK按钮。
4). 会自动选择和输入Build Target和Properties,可以按照默认的,不用修改。
5). 一个新建的测试工程(HelloWorldTest)就创建好了,如下图:
3. 创建一个Test Case
1). 在HelloWorldTest->src->com.liliandroid.helloworld.test上,右击选择New->Class,创建一个Test Case。
2). 在Name中输入Test Case的名字,如:UITextTest。
3). 创建好的Test Case如下图:
4. 导入robotium.jar
1). 选择测试工程HelloWorldTest右击,选择Build Path->Configure Build Path…
2). 在打开的Properties for HelloWorldTest中点击Add External JARs…按钮。
3). 因为之前创建的工程的SDK是2.2的,所以选择robotium-solo-1.8.0.jar。
id="iframe_0.06316110817715526" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.51testing.com/attachments/2010/11/115892_2010111711492218gV7.jpg?_=2128515%22%20style=%22border:none;max-width:1571px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.06316110817715526',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-style: none; border-width: initial; width: 734px; height: 568px;">
4). 点击OK按钮。
id="iframe_0.8600559264887124" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.51testing.com/attachments/2010/11/115892_201011171150121INXF.jpg?_=2128515%22%20style=%22border:none;max-width:1571px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8600559264887124',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-style: none; border-width: initial; width: 667px; height: 543px;">
5. 修改Test Case
打开UITextTest.java,输入以下内容并保存:
package com.liliandroid.helloworld.test;
import android.test.ActivityInstrumentationTestCase2;
import com.liliandroid.helloworld.ActivityMain;
import com.jayway.android.robotium.solo.Solo;
public class UITextTest extends ActivityInstrumentationTestCase2<ActivityMain>{
private Solo solo;
public UITextTest() {
super("com.liliandroid.helloworld", ActivityMain.class);
}
public void setUp()throwsException {
solo = newSolo(getInstrumentation(), getActivity());
}
public void testUI()throwsException {
boolean expected =true;
boolean actual =solo.searchText("This") && solo.searchText("is");
assertEquals("This and/or is are not found", expected, actual);
}
}
注:这个Case是测试运行了ActivityMain后的界面上是否有This和is这两个文字。
6. 运行测试程序
选择测试工程HelloWorldTest右击,选择Run As->Android JUnit Test运行测试程序
id="iframe_0.7548560556024313" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.51testing.com/attachments/2010/11/115892_201011171154471v8KZ.jpg?_=2128515%22%20style=%22border:none;max-width:1571px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7548560556024313',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-style: none; border-width: initial; width: 654px; height: 487px;">
运行完后的测试结果界面(Pass)
id="iframe_0.16672452772036195" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.51testing.com/attachments/2010/11/115892_201011171155311zhue.jpg?_=2128515%22%20style=%22border:none;max-width:1571px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.16672452772036195',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-style: none; border-width: initial; width: 428px; height: 202px;">
运行完后的测试结果界面(Fail)
id="iframe_0.12473941245116293" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.51testing.com/attachments/2010/11/115892_201011171156021mehX.jpg?_=2128515%22%20style=%22border:none;max-width:1571px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.12473941245116293',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-style: none; border-width: initial; width: 542px; height: 341px;">
注:
1. 被测试的工程和测试的工程可以是不同的SDK,但是不知道有什么影响。
2. 这是在eclipse中进行测试的,也可以在模拟器或者手机上测试。如果需要直接在模拟器或者手机上测试,需要将测试包和被测试包改成相同的签名。(关于apk包的签名和重签名,请参考本人日志《重新签名APK文件》)