---
title: "如何进行Android单元测试"
author: "rayloo"
date: "2011-02-24T13:39:00Z"
tags: []
slug: "android"
draft: false
lastmod: "2011-02-24T13:39:00Z"
id: 1008833
url: "https://rayloo.iteye.com/blog/1008833"
---

如何进行Android单元测试

1、Menifest.xml 中加入：  
&lt;application&gt; 中加入：

view plaincopy to clipboardprint?  
&lt;uses-library android:name="android.test.runner" /&gt;  
&lt;uses-library android:name="android.test.runner" /&gt;

&lt;application&gt; 外面加入：

view plaincopy to clipboardprint?  
&lt;uses-permission android:name="android.permission.RUN\_INSTRUMENTATION" /&gt;  
&lt;instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="name.feisky.android.test"  
android:label="Test for my app"/&gt;  
&lt;uses-permission android:name="android.permission.RUN\_INSTRUMENTATION" /&gt;  
&lt;instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="name.feisky.android.test"  
android:label="Test for my app"/&gt;

2、 编写单元测试代码：必须继承自 AndroidTestCase 类

view plaincopy to clipboardprint?  
package name.feisky.android.test;

import android.test.AndroidTestCase;  
import junit.framework.Assert;

public class MyTest extends AndroidTestCase {  
private static final String Tag="MyTest";

public void testSave() throws Throwable  
{  
int i=4+8;  
Assert.assertEquals(5,i);  
}

public void testSomethingElse() throws Throwable {  
Assert.assertTrue(1 + 1 == 12);  
}

}  
package name.feisky.android.test;

import android.test.AndroidTestCase;  
import junit.framework.Assert;

public class MyTest extends AndroidTestCase {  
private static final String Tag="MyTest";

public void testSave() throws Throwable  
{  
int i=4+8;  
Assert.assertEquals(5,i);  
}

public void testSomethingElse() throws Throwable {  
Assert.assertTrue(1 + 1 == 12);  
}

}

3、 执行测试

IntelliJ 中：

eclipse 中：右键 run as Android JUnit Test

命令行工具：

view plaincopy to clipboardprint?  
adb shell am instrument -w name.feisky.android.test/android.test.InstrumentationTestRunner  
adb shell am instrument -w name.feisky.android.test/android.test.InstrumentationTestRunner

也可以新建一个测试项目进行测试

1、 New &gt; Project &gt; Android &gt; Android Test Project .

添加测试用例类  
添加新类，基类设置为 android.test.ActivityInstrumentationTestCase2&lt;HelloAndroid&gt;

添加构造函数  
添加 setUp() 方法，这个方法在所有的测试之前进行变量和测试环境的初始化。

view plaincopy to clipboardprint?  
@Override  
protected void setUp() throws Exception {  
super.setUp();  
mActivity = this.getActivity();  
mView = (TextView) mActivity.findViewById(com.example.helloandroid.R.id.textview);  
resourceString = mActivity.getString(com.example.helloandroid.R.string.hello);  
}  
@Override  
protected void setUp() throws Exception {  
super.setUp();  
mActivity = this.getActivity();  
mView = (TextView) mActivity.findViewById(com.example.helloandroid.R.id.textview);  
resourceString = mActivity.getString(com.example.helloandroid.R.string.hello);  
}  
添加testPreconditions () 方法，检查初始化环境，只执行一次  
view plaincopy to clipboardprint?  
public void testPreconditions() {  
assertNotNull(mView);  
}  
public void testPreconditions() {  
assertNotNull(mView);  
}  
添加单元测试  
view plaincopy to clipboardprint?  
public void testText() {  
assertEquals(resourceString,(String)mView.getText());  
}  
public void testText() {  
assertEquals(resourceString,(String)mView.getText());  
}  
测试 Run As... &gt; Android JUnit Test