android應用的單元測試

來源:互聯網
上載者:User

標籤:android單元測試

1、在進行單元測試前首先必須AndroidManifest.xml中進行配置:


    (1)<!-- 在根節點 manifest節點下放置  指令集 instrumentation -->

        <instrumentation

            android:name="android.test.InstrumentationTestRunner"

            android:label="Tests for My App"

            android:targetPackage="com.xunfang.junit" />

            上面這些代表單元測試的架構的啟動裝置,啟動裝置有好幾類,預設使用上面的這種。


         targetPackage與上面的package相同,為的是使單元測試和當前應用處於同一進程中


    (2)<!--  使用的函數庫  application節點的下面-->

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


        代表單元測試的函數庫


附上代碼:

    AndroidManifest.xml中的配置:

 

    <uses-sdk

        android:minSdkVersion="14"

        android:targetSdkVersion="16" />

    <!-- 配置在manifest中 -->

    <instrumentation

        android:name="android.test.InstrumentationTestRunner"

        android:targetPackage="com.xunfang.junit" >

    </instrumentation>


    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <!-- 配置在application標籤中 -->

        <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>

     上面兩段有背景色的是配置通用檔案中的配置  


 商務邏輯中的代碼:

    

    public class MyMath {

    

    public int add(int a, int b ){

    return a + b;

    }

    }


    單元測試代碼:

    public class MyJuniteTestCase extends AndroidTestCase {

    

    public void test(){

    MyMath my = new MyMath() ;

    assertEquals(10, my.add(4, 6)) ;

    }

    }


測試步驟:

   (1) 項目--》右鍵--》Run As--》Android Jutil Test

   (2)開啟單元測試代碼--》在outLine中選擇你要測試的方法--》右鍵--》Run As--》Android Jutil Test

本文出自 “初學者” 部落格,請務必保留此出處http://10154241.blog.51cto.com/10144241/1663841

android應用的單元測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.