如何进行Android单元测试

CampC 2012-04-10

转自:http://www.cnblogs.com/feisky/archive/2010/07/23/1783826.html

#

Menifest.xml中加入:

<application>中加入:

<uses-libraryandroid:name="android.test.runner"/>

<application>外面加入:

<uses-permissionandroid:name="android.permission.RUN_INSTRUMENTATION"/>

<instrumentationandroid:name="android.test.InstrumentationTestRunner"android:targetPackage="name.feisky.android.test"

android:label="Testformyapp"/>

#

编写单元测试代码:必须继承自AndroidTestCase类

packagename.feisky.android.test;

importandroid.test.AndroidTestCase;

importjunit.framework.Assert;

publicclassMyTestextendsAndroidTestCase{

privatestaticfinalStringTag="MyTest";

publicvoidtestSave()throwsThrowable

{

inti=4+8;

Assert.assertEquals(5,i);

}

publicvoidtestSomethingElse()throwsThrowable{

Assert.assertTrue(1+1==12);

}

}

#

执行测试

IntelliJ中:

eclipse中:右键runasAndroidJUnitTest

命令行工具:

adbshellaminstrument-wname.feisky.android.test/android.test.InstrumentationTestRunner

相关推荐