qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          Android下junit單元測試、logCat的使用

           Android下junit單元測試
            軟件測試小知識:
            根據(jù)測試是否知道源代碼:
            黑盒測試:只關(guān)心程序執(zhí)行的過程和結(jié)果
            白盒測試:根據(jù)源代碼寫測試方法或者測試用例
            根據(jù)測試的粒度:
            方法測試:function test
            單元測試:unit test
            集成測試:intergration test
            根據(jù)測試的次數(shù):
            冒煙測試:smoke test(android 猴子)
            壓力測試:prssure test
            Android單元測試:
            1.Android測試類要繼承AndroidTestCase類
            2.寫測試方法,在測試方法內(nèi)使用斷言assert來測試要測試的方法
            3.在AndroidManifest.xml中,要設(shè)置
            <instrumentation
            android:name="android.test.InstrumentationTestRunner"
            android:targetPackage="com.lee.test" />
            和<uses-library android:name="android.test.runner" >
            4.確保adb連接正常。
            MyService.java

          package com.lee.test.service;

          public class MyService {

           /**
            * 計算器相加的業(yè)務(wù)
            * @param a
            * @param b
            * @return
            */
           public int add(int a,int b){
            return a+b;
           }
          }



           TestMyService.java
          package com.lee.test.service.test;
          import com.lee.test.service.MyService;
          import android.test.AndroidTestCase;
          public class TestMyService extends AndroidTestCase {
          /**
          * add方法的測試代碼
          * 把異常拋給測試框架
          * @throws Exception
          */
          public void testAdd()throws Exception{
          MyService myService = new MyService();
          int retVal = myService.add(3, 5);
          //斷言,預(yù)期結(jié)果是8,實際結(jié)果是retVal
          assertEquals(8, retVal);
          }
          }
          package com.lee.test.service.test;
          import com.lee.test.service.MyService;
          import android.test.AndroidTestCase;
          public class TestMyService extends AndroidTestCase {
          /**
          * add方法的測試代碼
          * 把異常拋給測試框架
          * @throws Exception
          */
          public void testAdd()throws Exception{
          MyService myService = new MyService();
          int retVal = myService.add(3, 5);
          //斷言,預(yù)期結(jié)果是8,實際結(jié)果是retVal
          assertEquals(8, retVal);
          }
          }

            AndroidManifest.xml
          <?xml version="1.0" encoding="utf-8"?>
          <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.lee.test"
          android:versionCode="1"
          android:versionName="1.0" >
          <uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="17" />
          <!-- 這里是進(jìn)行單元測試必須要添加,指令集必須在manifest節(jié)點下 -->
          <instrumentation
          android:name="android.test.InstrumentationTestRunner"
          android:targetPackage="com.lee.test" />
          <application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" >
          <activity
          android:name="com.lee.test.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>
          <!-- 這里也是進(jìn)行單元測試必須要添加,在application節(jié)點下,使用函數(shù)庫 -->
          <uses-library android:name="android.test.runner" >
          </uses-library>
          </application>
          </manifest>
          <?xml version="1.0" encoding="utf-8"?>
          <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.lee.test"
          android:versionCode="1"
          android:versionName="1.0" >
          <uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="17" />
          <!-- 這里是進(jìn)行單元測試必須要添加,指令集必須在manifest節(jié)點下 -->
          <instrumentation
          android:name="android.test.InstrumentationTestRunner"
          android:targetPackage="com.lee.test" />
          <application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" >
          <activity
          android:name="com.lee.test.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>
          <!-- 這里也是進(jìn)行單元測試必須要添加,在application節(jié)點下,使用函數(shù)庫 -->
          <uses-library android:name="android.test.runner" >
          </uses-library>
          </application>
          </manifest>


            logCat的使用
            日志信息是分等級的:
            verbose:提醒  黑色
            debug:調(diào)試  藍(lán)色
            info:信息  綠色
            warn:警告  橙色
            error:錯誤  紅色
            使用上面的程序,可以做一個demo
            修改MyService.java

          package com.lee.test.service;
          import android.util.Log;
          public class MyService {
          private static final String TAG = "MyService";
          /**
          * 計算器相加的業(yè)務(wù)
          * @param a
          * @param b
          * @return
          */
          public int add(int a,int b){
          Log.v(TAG, ""+a);
          Log.d(TAG, ""+b);
          Log.i(TAG, ""+b);
          Log.w(TAG, ""+a);
          Log.e(TAG, ""+b);
          System.out.println("System.out的log");
          System.err.println("System.err的log");
          return a+b;
          }
          }
          package com.lee.test.service;
          import android.util.Log;
          public class MyService {
          private static final String TAG = "MyService";
          /**
          * 計算器相加的業(yè)務(wù)
          * @param a
          * @param b
          * @return
          */
          public int add(int a,int b){
          Log.v(TAG, ""+a);
          Log.d(TAG, ""+b);
          Log.i(TAG, ""+b);
          Log.w(TAG, ""+a);
          Log.e(TAG, ""+b);
          System.out.println("System.out的log");
          System.err.println("System.err的log");
          return a+b;
          }
          }


          posted on 2013-10-14 10:16 順其自然EVO 閱讀(793) 評論(0)  編輯  收藏 所屬分類: android

          <2013年10月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 黎平县| 南江县| 尖扎县| 弋阳县| 武邑县| 高青县| 廊坊市| 江川县| 陆丰市| 察隅县| 东丽区| 肥东县| 金溪县| 丹江口市| 安塞县| 克什克腾旗| 福鼎市| 永丰县| 巢湖市| 江川县| 朝阳县| 唐山市| 长治市| 扎囊县| 连城县| 沽源县| 揭阳市| 鹤峰县| 青阳县| 图木舒克市| 乌兰察布市| 武夷山市| 弥渡县| 乐平市| 宁夏| 乐清市| 三门县| 巩义市| 南投县| 伊吾县| 徐闻县|