本文版权所有,欢迎转载,转载请注明来源: http://mdev.cc/dev
Andorid下使用Junit测试框架,是步入正规的Androdid开发的必经之路,在Junit中可以得到组件,可以模拟发送事件,检测程序处理的正确性,下面就开始我们的教程:
- 工具:
- 1、Android1.5 SDK
- 2、ADT 0.9
- 3、Eclipse
工具:
1、Android1.5 SDK
2、ADT 0.9
3、Eclipse
- 需要的知识:
- 1、 Android开发的基础
- 2、Junit的基础
需要的知识:
1、 Android开发的基础
2、Junit的基础
一、
首先建立工程:
目录:
选中的test source folder是测试类的包,包名随便,但是在配置
Manifest.xml要注意

二、配置/layout/main.xml文件,加入两个button组件,代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button android:text="Button01" android:id="@+id/Button01"
- android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
- <Button android:text="Button02" android:id="@+id/Button02"
- android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
- </LinearLayout>
三、被测试的Activity代码
- package cc.androidos.activity;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.Button;
- public class NewActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- public int add(int a , int b){
- return a+b;
- }
- }
package cc.androidos.activity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class NewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public int add(int a , int b){
return a+b;
}
}
四、测试NewActivity代码,这里要继承ActivityInstrumentationTestCase2,ActivityInstrumentationTestCase2是TestCase的子类
- package cc.andoridos.activity.test;
- import cc.androidos.activity.NewActivity;
- import cc.androidos.activity.R;
- import android.test.ActivityInstrumentationTestCase2;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.widget.Button;
- public class TestNewActivity extends
- ActivityInstrumentationTestCase2<NewActivity> {
- private Button button1 = null;
- private Button button2 = null;
- private NewActivity newActivity = null;
- public TestNewActivity() {
- super("cc.androidos.activity", NewActivity.class);
- //This first parameter should the Activity package
- //if other , the junit give us the exception: unable resolve the activity
- }
- @Override
- protected void setUp() throws Exception {
- String tag = "setUp";
- Log.e(tag, "init all var....");
- newActivity = getActivity();
- button1 = (Button) newActivity.findViewById(R.id.Button01);
- button2 = (Button) newActivity.findViewById(R.id.Button02);
- }
- /**
- * Testing the button is focused or not
- */
- public void testButtonFocus() {
- String tag = "testButtonFocus";
- Log.e(tag, "start the button focus...");
- assertTrue("Button1 is focused", button1.isFocused());
- Log.e(tag, "send the down key event...");
- sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
- assertTrue("Button2 is focused", button2.isFocused());
- }
- /**
- * Testing the add method in actvity
- */
- public void testAdd(){
- String tag ="testAdd";
- Log.e(tag, "Test the add method in NewActivity...");
- int i = newActivity.add(2, 5);
- assertEquals(7, i);
- }
- }
package cc.andoridos.activity.test;
import cc.androidos.activity.NewActivity;
import cc.androidos.activity.R;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
public class TestNewActivity extends
ActivityInstrumentationTestCase2<NewActivity> {
private Button button1 = null;
private Button button2 = null;
private NewActivity newActivity = null;
public TestNewActivity() {
super("cc.androidos.activity", NewActivity.class);
//This first parameter should the Activity package
//if other , the junit give us the exception: unable resolve the activity
}
@Override
protected void setUp() throws Exception {
String tag = "setUp";
Log.e(tag, "init all var....");
newActivity = getActivity();
button1 = (Button) newActivity.findViewById(R.id.Button01);
button2 = (Button) newActivity.findViewById(R.id.Button02);
}
/**
* Testing the button is focused or not
*/
public void testButtonFocus() {
String tag = "testButtonFocus";
Log.e(tag, "start the button focus...");
assertTrue("Button1 is focused", button1.isFocused());
Log.e(tag, "send the down key event...");
sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
assertTrue("Button2 is focused", button2.isFocused());
}
/**
* Testing the add method in actvity
*/
public void testAdd(){
String tag ="testAdd";
Log.e(tag, "Test the add method in NewActivity...");
int i = newActivity.add(2, 5);
assertEquals(7, i);
}
}
五、配置Manifest.xml文件
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="cc.androidos.activity" android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".NewActivity" android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".NewActivity2" android:label="@string/app_name">
- </activity>
- <uses-library android:name="android.test.runner" />
- <!-- Loading test library -->
- </application>
- <uses-sdk android:minSdkVersion="3" />
- <!--这个是关键,android:targetPackage="cc.androidos.activity"要测试的包,android:name="android.test.InstrumentationTestRunner" 用于跑TestCase的类-->
- <instrumentation android:targetPackage="cc.androidos.activity"
- android:label="Test New Activty" android:name="android.test.InstrumentationTestRunner"></instrumentation>
- </manifest>
六、运行模拟器和项目
七、在TestCase类中点击右键:

八、运行结果

九、不使用Eclipse,控制台的命令:
E:/
android/
android-sdk-windows-1.5_pre/tools>adb shell am instrument -w cc.andro
idos.activity/ android. test.InstrumentationTestRunner
idos.activity/ android. test.InstrumentationTestRunner
如果你配置了
Android环境变量,直接使用:
adb shell am instrument -w cc.androidos.activity/ android. test.InstrumentationTestRunner
语法:adb shell am instrument -w <被测试的类的包名>/ android. test.InstrumentationTestRunner
参数参考帮助文档
adb shell am instrument -w cc.androidos.activity/ android. test.InstrumentationTestRunner
语法:adb shell am instrument -w <被测试的类的包名>/ android. test.InstrumentationTestRunner
参数参考帮助文档