您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > junit
Android下Junit測(cè)試框架配置
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2016/3/18 11:41:56 ] 推薦標(biāo)簽:單元測(cè)試工具 軟件測(cè)試

  我們的業(yè)務(wù)代碼一般是放在一個(gè)新的包下面,這個(gè)業(yè)務(wù)類(lèi)不能夠通過(guò)右鍵run as java application,因?yàn)閍ndroid項(xiàng)目只能運(yùn)行在手機(jī)上的dalvak虛擬機(jī)里面。
  新建一個(gè)包,里面寫(xiě)測(cè)試類(lèi),測(cè)試類(lèi)需要繼承AndroidTestCase類(lèi),寫(xiě)測(cè)試方法,需要throws exception拋出異常給測(cè)試框架,測(cè)試方法里面一般new出需測(cè)試的類(lèi),調(diào)用它的方法,然后斷言結(jié)果,assertEquals(預(yù)估, 實(shí)際結(jié)果)
  在outline視窗 (window=>show view=>outline)里面選中該方法右鍵run as android junit test
  此時(shí)會(huì)報(bào)錯(cuò)
  [2016-02-27 21:29:54 - 單元測(cè)試] 單元測(cè)試 does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library android.test.runner in its AndroidManifest.xml
  需要在清單文件里面配置instrumentation指令集
<!-- 指令集在manifest節(jié)點(diǎn)下 -->
<!-- 測(cè)試用例,名稱(chēng)是固定的,目標(biāo)包名 -->
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.tsh.junit" >
</instrumentation>
<!-- 使用的函數(shù)庫(kù),在application節(jié)點(diǎn)下 -->
<uses-library android:name="android.test.runner"/>
  Xml里面寫(xiě)注釋 ctrl+shift+/,
  Junit里面打印出綠條,說(shuō)明沒(méi)有錯(cuò)誤,如果有錯(cuò)誤打印紅色的條,錯(cuò)誤追逐里面有錯(cuò)誤信息,如斷言錯(cuò)誤等,如果清單文件里面的信息記不住,那么請(qǐng)這樣操作,new => project =>android android test project => select test target 完成以后會(huì)有個(gè)項(xiàng)目里面有清單文件
  java代碼:
package com.tsh.junit.test;
import com.tsh.junit.service.CalcService;
import android.test.AndroidTestCase;
public class CalServiceTest extends AndroidTestCase {
public void testAdd() throws Exception{
CalcService service=new CalcService();
int res=service.add(3, 5);
assertEquals(8, res);
}
}
  清單文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tsh.junit"
android:versionCode="1"
android:versionName="1.0" >
<!-- 指令集在manifest節(jié)點(diǎn)下 -->
<!-- 測(cè)試用例,名稱(chēng)是固定的,目標(biāo)包名 -->
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.tsh.junit" >
</instrumentation>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- 使用的函數(shù)庫(kù),在application節(jié)點(diǎn)下 -->
<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>
</manifest>

軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd