如何对Andrioid进行单元测试

来源:互联网 发布:java 保存文本文件 编辑:程序博客网 时间:2024/05/01 16:15
<manifest xmlns:android="http://schemas.android.com/apk/res/android"        package="com.pccw"        android:versionCode="1"        android:versionName="1.0">      <uses-sdk android:minSdkVersion="8" />      <application android:icon="@drawable/icon" android:label="@string/app_name">          <activity android:name=".MainActivity"                    android:label="@string/app_name">              <intent-filter>                  <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />              </intent-filter>          </activity>          <!—添加代码1-->          <uses-library android:name="android.test.runner"/>  </application>      <!—添加代码2-->          <instrumentation android:name="android.test.InstrumentationTestRunner"              android:targetPackage="com.pccw" android:label="aaa"/>  </manifest>  

编写业务逻辑,即需要被测试的模块

public class PersonService {      public void save(String name){          String sub = name.substring(6);      }      public int add(int a, int b){          return a+b;      }  }  

编写单元测试代码

public class PersonServiceTest extends AndroidTestCase {      public void testSave() throws Exception {          PersonService service = new PersonService();          service.save(null);      }      public void testAdd() throws Exception {          PersonService service = new PersonService();          int result = service.add(1, 2);          Assert.assertEquals(3, result);      }  }  

最好是新建项目other->Android->Android Test project
然后直接去复制:

0 0