android基礎(6):junit測試

來源:互聯網
上載者:User

標籤:android

一:測試的基本概念

根據源碼:

黑箱測試:注重過程和結果

白盒測試:根據源碼寫測試方法

 

測試的粒度:

方法測試:function test

單元測試:unit test

整合測試:

 

根據次數

煙霧測試 (Smoke Test)

壓力測試

 

二:搭建自己的測試架構

1.      業務代碼

 

publicclass CalcService {

    publicint add(int x,int y){

       return x+y;

    }

}

2.  測試代碼

 

publicclass TestCalcService extends AndroidTestCase {

    publicvoid testAdd()throws Exception{

       CalcService service=new CalcService();

       int sum=service.add(3, 5);

       assertEquals(8, sum);

    }

}

 

3.  搭建測試架構

<?xml version="1.0"encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.android3"

   android:versionCode="1"

   android:versionName="1.0" >

    <!-- 指令集需要在mainfest節點下 -->

         <instrumentation

            android:name="android.test.InstrumentationTestRunner"

            android:targetPackage="com.example.android3"

            />

        

   <uses-sdk

       android:minSdkVersion="8"

       android:targetSdkVersion="18" />

   <application

       android:allowBackup="true"

       android:icon="@drawable/ic_launcher"

       android:label="@string/app_name"

       android:theme="@style/AppTheme" >

       

       <!-- 在application節點下使用函數庫 -->

        <uses-libraryandroid:name="android.test.runner"/>

    

       <activity

           android:name="com.example.android3.MainActivity"

           android:label="@string/app_name" >

           <intent-filter>

                <actionandroid:name="android.intent.action.MAIN" />

                <categoryandroid:name="android.intent.category.LAUNCHER" />

           </intent-filter>

       </activity>

   </application>

</manifest>

 

三:建立測試專案


四:測試的日誌資訊

Verbose: 提醒

Debug: 調試

Info:   資訊

Warn:  警告

Error:  錯誤

 

1.      修改上面的業務代碼

public class CalcService {

         privateString tag="CalcService";

         publicint add(int x,int y){

                   Log.v(tag, "x="+x);

                   Log.d(tag, "y="+y);

                   int result=x+y;

                   Log.i(tag,"result="+result);

                   Log.w(tag,"result="+result);

                   Log.e(tag,"result="+result);

                   returnx+y;

         }

}

 

 

2.      添加測試過濾器



1.      運行測試代碼

 

記錄檔顯示:


著作權聲明:博主原創文章,轉載請說明出處。http://blog.csdn.net/dzy21

android基礎(6):junit測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.