溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Robotium編寫測(cè)試用例如何模擬Junit4的BeforeClass和AfterClass方法1 - 條件判斷法

發(fā)布時(shí)間:2020-07-31 07:14:02 來源:網(wǎng)絡(luò) 閱讀:558 作者:zhukev 欄目:移動(dòng)開發(fā)

Robotium的測(cè)試類ActivityInstrumentationTestCase2是繼承于Junit3的TestCase類,所以并沒有提供Junit4的特性.如網(wǎng)上總結(jié)說的

  • 不能通過annotate的方式來識(shí)別子類的新特征,如不能實(shí)現(xiàn)@beforeclass,@afterclass等特征。只能通過寫setup和teardown,
  • TestCase只能以test開頭進(jìn)行測(cè)試case書寫。
那么有時(shí)我們并不想每次開始/完成一個(gè)case的時(shí)候都做一些重復(fù)的動(dòng)作,也就是要實(shí)現(xiàn)Junit4的@beforeclass和@afterclass,該怎么辦呢?
以SDK自帶的Notepad測(cè)試用例作為例子,假如現(xiàn)在我們需要實(shí)現(xiàn)兩個(gè)測(cè)試用例
  • testAddNoteCNTittle:創(chuàng)建一個(gè)中文標(biāo)題的筆記
  • testAddNoteEngTitle:創(chuàng)建一個(gè)英文標(biāo)題的筆記
根據(jù)實(shí)例提供的代碼,在setup里面會(huì)初始化solo而在teardown里面會(huì)關(guān)閉所有打開的activities,也就是說每執(zhí)行一個(gè)case都會(huì)重新初始化一次solo和關(guān)閉所有的activities:
	@Override 	public void setUp() throws Exception { 		//setUp() is run before a test case is started.  		//This is where the solo object is created. 		super.setUp();  		this.activity = this.getActivity();  		this.solo = new Solo(getInstrumentation(), getActivity()); 	} 	 	@Override 	public void tearDown() throws Exception { 		//tearDown() is run after a test case has finished.  		//finishOpenedActivities() will finish all the activities that have been opened during the test execution. 		solo.finishOpenedActivities(); 	} 
但事實(shí)上我們?cè)谶@個(gè)腳本只是去創(chuàng)建兩個(gè)Note,并不需要每執(zhí)行完一個(gè)case都要去初始化solo和關(guān)閉所有activities。google后沒有發(fā)現(xiàn)有現(xiàn)成的取代@beforeclass和@aferclass的方法。
以下本人的實(shí)現(xiàn)方法
<pre name="code" class="java">package com.example.android.notepad.test;  import com.robotium.solo.Solo;  import android.test.ActivityInstrumentationTestCase2; import android.app.Activity;  @SuppressWarnings("rawtypes") public class TCCreateNote extends ActivityInstrumentationTestCase2{  	private static Solo solo = null; 	public Activity activity; 	 <span style="white-space:pre">	</span>private static final int NUMBER_TOTAL_CASES = 2; 	private static int run = 0; 	 	private static Class<?> launchActivityClass;  	//對(duì)應(yīng)re-sign.jar生成出來的信息框里的兩個(gè)值 	private static String mainActiviy = "com.example.android.notepad.NotesList"; 	private static String packageName = "com.example.android.notepad";  	static {  		try {  			launchActivityClass = Class.forName(mainActiviy);  		} catch (ClassNotFoundException e) {  			throw new RuntimeException(e);  		}  	} 	 	 	@SuppressWarnings("unchecked") 	public TCCreateNote() { 		super(packageName, launchActivityClass); 	}  	 	@Override 	public void setUp() throws Exception { 		//setUp() is run before a test case is started.  		//This is where the solo object is created. 		super.setUp();  <span style="white-space:pre">		</span>//The variable solo has to be static, since every time after a case's finished, this class TCCreateNote would be re-instantiated 		// which would lead to soto to re-instantiated to be null if it's not set as static 		if(solo == null) { 			TCCreateNote.solo = new Solo(getInstrumentation(), getActivity()); 		} 	} 	 	@Override 	public void tearDown() throws Exception { 		//Check whether it's the last case executed. 		run += countTestCases(); 		if(run >= NUMBER_TOTAL_CASES) { 			solo.finishOpenedActivities(); 		} 	}  	public void testAddNoteCNTitle() throws Exception { 		 		solo.clickOnMenuItem("Add note"); 		solo.enterText(0, "中文標(biāo)簽筆記"); 		solo.clickOnMenuItem("Save"); 		solo.clickInList(0); 		solo.clearEditText(0); 		solo.enterText(0, "Text 1"); 		solo.clickOnMenuItem("Save"); 		solo.assertCurrentActivity("Expected NotesList Activity", "NotesList"); 		 		solo.clickLongOnText("中文標(biāo)簽筆記"); 		solo.clickOnText("Delete"); 	} 	 	 	public void testAddNoteEngTitle() throws Exception { 		solo.clickOnMenuItem("Add note"); 		solo.enterText(0, "English Title Note"); 		solo.clickOnMenuItem("Save"); 		solo.clickInList(0); 		solo.clearEditText(0); 		solo.enterText(0, "Text 1"); 		solo.clickOnMenuItem("Save"); 		solo.assertCurrentActivity("Expected NotesList Activity", "NotesList"); 		 		solo.clickLongOnText("English Title Note"); 		solo.clickOnText("Delete"); 	} }


  

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI