单元测试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>
相关文章推荐
- android学习二(Activity)
- Android布局动画
- Android基础部分再学习---全面理解Fragment
- Android studio 百度地图开发(6)Marker绑定事件、计算两点距离
- Androidmanifest.xml在Android项目中的作用
- Android 无线调试
- Android蓝牙通信
- Android API讲解之:ByteArrayOutputStream
- android BitmapShader类
- Android动画四:View的animate方法 (ViewPropertyAnimator)
- Android简易指南针
- Android中的图片压缩
- android 传感器(OnSensorChanged)使用介绍
- Android Shape 形状
- Android 拖放(OnDragListener)
- android coredump 调试
- 框架选择
- Android Studio下OpenCV及JNI开发
- Android中通过url获取图片Bitmap
- Android笔记37: Sensor.TYPE_ORIENTATION 方向传感器,转向晕了个头