qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

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

           Android下junit單元測試
            軟件測試小知識:
            根據測試是否知道源代碼:
            黑盒測試:只關心程序執行的過程和結果
            白盒測試:根據源代碼寫測試方法或者測試用例。
            根據測試的粒度:
            方法測試:function test
            單元測試:unit test
            集成測試:intergration test
            根據測試的次數:
            冒煙測試:smoke test(android 猴子)
            壓力測試:prssure test
            Android單元測試:
            1.Android測試類要繼承AndroidTestCase類
            2.寫測試方法,在測試方法內使用斷言assert來測試要測試的方法
            3.在AndroidManifest.xml中,要設置
            <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 {

           /**
            * 計算器相加的業務
            * @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);
          //斷言,預期結果是8,實際結果是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);
          //斷言,預期結果是8,實際結果是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" />
          <!-- 這里是進行單元測試必須要添加,指令集必須在manifest節點下 -->
          <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>
          <!-- 這里也是進行單元測試必須要添加,在application節點下,使用函數庫 -->
          <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" />
          <!-- 這里是進行單元測試必須要添加,指令集必須在manifest節點下 -->
          <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>
          <!-- 這里也是進行單元測試必須要添加,在application節點下,使用函數庫 -->
          <uses-library android:name="android.test.runner" >
          </uses-library>
          </application>
          </manifest>


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

          package com.lee.test.service;
          import android.util.Log;
          public class MyService {
          private static final String TAG = "MyService";
          /**
          * 計算器相加的業務
          * @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";
          /**
          * 計算器相加的業務
          * @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

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 宜良县| 长寿区| 宾阳县| 新和县| 伊通| 平江县| 叶城县| 肥东县| 开江县| 罗源县| 胶州市| 盈江县| 墨脱县| 镇原县| 民丰县| 葵青区| 玛多县| 安泽县| 正蓝旗| 吉林市| 右玉县| 洛扎县| 汶川县| 宜良县| 定陶县| 巴林右旗| 蒙城县| 台东市| 达州市| 滨州市| 高平市| 岳阳县| 嘉荫县| 琼海市| 南皮县| 武城县| 铜梁县| 卢龙县| 仙游县| 荣成市| 江津市|