<application>中加入: android
<uses-library android:name="android.test.runner" /> shell
<application>外面加入: app
<uses-permission android:name="android.permission.RUN_INSTRUMENTATION" /> eclipse
<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="name.feisky.android.test" ide
android:label="Test for my app"/> 函數
package name.feisky.android.test; 工具
import android.test.AndroidTestCase; 單元測試
import junit.framework.Assert; 測試
public class MyTest extends AndroidTestCase { this
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);
}
}
IntelliJ中:
eclipse中:右鍵 run as Android JUnit Test
命令行工具:
adb shell am instrument -w name.feisky.android.test/android.test.InstrumentationTestRunner
也能夠新建一個測試項目進行測試
添加新類,基類設置爲android.test.ActivityInstrumentationTestCase2<HelloAndroid>
添加setUp()方法,這個方法在全部的測試以前進行變量和測試環境的初始化。
@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);
}
public void testPreconditions() {
assertNotNull(mView);
}
public void testText() {
assertEquals(resourceString,(String)mView.getText());
}