一、Android的测试
1、待测试方法类
public class Function {
public int add(int a, int b) {
int sum = a + b;
return sum;
}
public int jian(int a, int b) {
int jians = a - b;
return jians;
}
}
2、测试方法类
//需要继承AndroidTestCase类
public class FunctionTest extends AndroidTestCase{
public void testAdd() throws Exception{
Function js = new Function();
int sum = js.add(1, 2);
Assert.assertEquals(3, sum);
}
public void testJian() throws Exception{
Function js = new Function();
int sum = js.jian(2, 1);
Assert.assertEquals(2, sum);
}
}
3、清单文件配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myviewgroup"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- 配置 -->
<uses-library android:name="android.test.runner" />
<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>
</application>
<!-- 配置 包名 -->
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:label="Test for my app"
android:targetPackage="com.example.myviewgroup" />
</manifest>
4、运行 Run As -->Android Junit Test
将所有方法的运行结果进行显示,报错或者正确
二、Java的测试
1、项目添加lib junit-4.8.2.jar
2、待测试方法类
public class GoodFunction {
public int add(int a, int b) {
int sum = a + b;
return sum;
}
}
3、测试方法类
public class GoodFunctionTest {
@Test
public void testadd(){
int add = new GoodFunction().add(1, 2);
Assert.assertEquals("加法问题",3,add);
}
}