您的位置:首页 > 移动开发 > Android开发

单元测试1(Androidf)

2016-04-04 21:59 489 查看



步骤:

1.确定要测试的类(MathUtils)方法(incrment)。 --incrment

不能直接在MathUtils类中的main方法直接测试,会有异常,如下:

If you would like to submit a bug report, please visit:
http://java.sun.com/webapps/bugreport/crash.jsp#
2.测试incrment方法,Test类需要继承AndroidTestCase

此时不能测试,测试会有如下错误:

JunitTest does not specify a android.test.InstrumentationTestRunner instrumentation or

does not declare uses-library android.test.runner in its AndroidManifest.xml

3.在清单文件中指定测试信息和要测试的包, 指定引用的测试包

1.

<span style="font-size:14px;">package com.ithema28.junittest.test;

import com.ithema28.junittest.utlis.MathUtils;

import android.test.AndroidTestCase;

public class Test extends AndroidTestCase{

public void test(){
//	System.out.println("test dsa ");

int result = MathUtils.incrment(9, 10);
//断言  ,断定某一个对象就是某一个值
assertEquals(19, result);
}
/*
JunitTest does not specify a android.test.InstrumentationTestRunner instrumentation or
does not declare uses-library android.test.runner in its AndroidManifest.xml
*/

}
</span>
2.

<span style="font-size:14px;">package com.ithema28.junittest.utlis;

import android.util.Log;

public class MathUtils {

/**
* 加法运算
* */
public static int incrment(int x,int y){
/*
这几个BUG依次增强
*/
Log.v("MathUtils", "黑色:" + x + " + " +   y   + " = " + (x+y));
Log.d("MathUtils","蓝色:" + x + " + " +   y   + " = " + (x+y));
Log.i("MathUtils", "绿色:" +x + " + " +   y   + " = " + (x+y));
Log.w("MathUtils","黄色:" + x + " + " +   y   + " = " + (x+y));
Log.e("MathUtils","红色:" + x + " + " +   y   + " = " + (x+y));

return x+y;
}
/*
这样测试不对
If you would like to submit a bug report, please visit: http://java.sun.com/webapps/bugreport/crash.jsp# * */
/*
public static void main(String[] args) {

MathUtils m = new MathUtils();
int result = m.incrment(1,2);
System.out.println(result);
}
*/

}
</span>


3.
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ithema28.junittest"
android:versionCode="1"
android:versionName="1.0" >

<!--     指定测试信息和要测试的包 -->
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.ithema28.junittest"></instrumentation>

<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>

</manifest>



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: