全部博文(322)
分类: Java
2011-01-05 17:33:03
Menifest.xml中加入:
android:label="Test for my app"/>
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);
}
}
IntelliJ中:
eclipse中:右键 run as Android JUnit Test
命令行工具:
adb shell am instrument -w name.feisky.android.test/android.test.InstrumentationTestRunner
也可以新建一个测试项目进行测试
添加新类,基类设置为android.test.ActivityInstrumentationTestCase2
添加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());
}