Android Junit測試環境搭建

來源:互聯網
上載者:User

標籤:android   junit   單元測試   junit環境搭建   

一.單元測試簡介

在android的開發的過程中,經常需要對業務方面的代碼進行測試,熟悉java的人都知道junit, 在android中google提供了基於junit為android進行了最佳化得自動化架構junit,在Junit中可以得到組件,可以類比發送事件和檢測程式處理的正確性,在java中使用junit編寫測試案例,我們需要繼承TestCase,在android中需要繼承AndroidTestCase。

二.環境搭建

1. 業務類

業務類中寫了add函數,Junit來測試add函數的正確性。

 

<span style="font-family:Times New Roman;">package com.mxy.service;public class CalcService {public int add(int a,int b) {System.out.println("執行了加方法");return a + b;}}</span>

2. 測試類別

測試類別繼承了AndroidTestCase 類,寫了testAdd函數,注意測試類別中函數必須以test開頭,返回值為void,類型為public 。

 

<span style="font-family:Times New Roman;">package com.mxy.test;import com.mxy.service.CalcService;import android.test.AndroidTestCase;public class CalcServiceTest extends AndroidTestCase {public void testAdd() {CalcService cal = new CalcService();int result = cal.add(5,3);System.out.println(result);assertEquals(8, result);}}</span>

3. 配置AndroidManifeset.xml

在<applicetion />標籤中添加:

 

<span style="font-family:Times New Roman;"><uses-library android:name="android.test.runner"/></span>

在<mainfest />標籤中添加:

 

<span style="font-family:Times New Roman;"><instrumentation android:name="android.test.InstrumentationTestRunner"            android:targetPackage="com.uicc.cloudservice" android:label="Test for my app"/></span>

android:targetPackage的包名必須和manifest 標籤的package一致。

三.問題及解決方案

1.  錯誤:Warning: No instrumentation runner found for the launch, using   android.test.InstrumentationTestRunner.  

原因:模擬器不能記住Androidmanifest的配置,在運行時需要重新設定回合組態。

解決步驟如下:  

(1).在工程或者測試類別名字上點擊右鍵,選擇properties  
(2).在Run/Debug setting中選擇要啟動並執行工程名字,點擊右邊的Edit,進入Edit launch configuration properties介面,在Test選項卡中的instrumentation runner後面的下拉式清單中選擇android.test.InstrumentationTestRunner  。

(3).重新運行該測試單元即可。 

2. android:targetPackage的包名必須和manifest 標籤的package一致。否則會出現找不到包的錯誤。

四.參考網址

1.  http://blog.csdn.net/mengxiangyue/article/details/20568433 環境搭建

2. http://blog.csdn.net/you12345678901234567/article/details/7632807 錯誤解決方案

 

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