Android之單元測試

來源:互聯網
上載者:User

在實際開發中,開發android軟體的過程需要不斷地進行測試。使用Junint測試架構,是正規Android開發的必用技術,在Junint中可以得到組件,可以類比發送事件和檢測程式處理的正確性。單元測試是嵌入到項目中;也可以作為一個單獨的項目爭對某個具體項目進行測試。

 

第一步:首先在AndroidManifest.xml中加入下面紅色代碼:

 

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

package="com.lee0000.test" android:versionCode="1" android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

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

</application>

<use-sdk android:minSdkVersion="6"/>

<instrumentation android:name="android.test.instrumentationTestRunner" android:targetPackage="com.lee0000.test" android:label="Tests"/> 

 ***上面targetPackage指定的包要和應用的package相同。

 

第二步:編寫單元測試代碼,一般對將要測試的方法命名testXXX。需要測試的時候選擇大綱(Outline視圖)選擇測試的方法右鍵點擊,選擇"Run As" - "Android Junit Test"。

例,

項目結構: 

 

 

AndroidManifest.xml檔案: 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.lee0000.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".JUintTestActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    <uses-library android:name="android.test.runner" />       
    </application>
    <instrumentation 
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.lee0000.test" android:label="Tests"/>   

</manifest>  


定義測試的兩個方法:

public class testclass {
    public void str(String s){
        System.out.println(s.substring(6));
    }
    
    public int add(int a,int b){
        return a+b;
    }

}  

一般繼承的是AndroidTestCase,測試的時候就是測試這兩個方法,如果在對應方法中選擇"Run As" - "Android Junit Test"時出錯,可以右鍵Test類,選擇"Run as" - "Run Configurations",在 Instrumentation runner中選擇:

import junit.framework.Assert;
import android.test.AndroidTestCase;
public class Test extends AndroidTestCase{
    public void teststr() throws Exception{
        testclass tc = new testclass();
        tc.str("null");
    }
    
    public void testadd(){
        testclass tc = new testclass();
        int t = tc.add(1, 2);
        Assert.assertEquals(3, t);
    }

}  

 

相關文章

聯繫我們

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