package com.itheima28.junittest.utils;
import android.util.Log;
public class MathUtils {
/**
* 加法运算
* @param x
* @param y
* @return
*/
public static int incrment(int x, int y) {
System.out.println(x + " + " + y + " = " + (x + y));
System.err.println(x + " + " + y + " = " + (x + y));
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;
}
// public static void main(String[] args) {
// MathUtils m = new MathUtils();
// int result = m.incrment(1, 10);
// System.out.println(result);
// }
}
Debug调试。
使用另外一个工程进行单元测试
新建一个工程——AndroidTestProject
----------------------------小结---------------------------
android下单元测试:
在AndroidManifest.xml文件中配置一下信息:
在manifest节点下:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.itheima28.junittest" />
在application节点下配置下面信息:
<uses-library android:name="android.test.runner" />
测试时, 定义一个类继承AndroidTestCase
对应用进行单元测试
在实际开发中,开发android软件的过程需要不断地进行测试。而使用Junit测试框架,侧是正规Android开发的必用技术,在Junit
中可以得到组件,可以模拟发送事件和检测程序处理的正确性。
第一步:首先在AndroidManifest.xml中加入下面红色代码:
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="cn.itcast.action“android:versionCode="1“ android:versionName="1.0">
<application android:icon="@drawable/icon"android:label="@string/app_name">
<uses-library android:name="android.test.runner"/>
....
</application>
<uses-sdkandroid:minSdkVersion="6"/>
<instrumentationandroid:name="android.test.InstrumentationTestRunner"
android:targetPackage="cn.itcast.action"android:label="Tests for My App" />
</manifest>
上面targetPackage指定的包要和应用的package相同。
第二步:编写单元测试代码(选择要测试的方法,右键点击“RunAs”--“AndroidJunitTest”):
importandroid.test.AndroidTestCase;
importandroid.util.Log;
publicclass XMLTestextendsAndroidTestCase{
publicvoid testSomething()throwsThrowable{
Assert.assertTrue(1+ 1 == 3);
}
}