Robotium編寫測試用例如何模擬Junit4的BeforeClass和AfterClass方法2 - SingleLaunchActivityTestCase

在上一遍筆記博客中本覺得只能在Setup和TearDown中作條件判斷來實現Junit4的@BeforeClass和@AfterClass功能。今天查看SDK時發現實際上是有現成的方法來實現這個功能的。java

方法就是把編寫的測試用例從繼承自ActivityInstrumentationTestCase2改爲繼承成SingleLaunchActivityTestCase。android

爲何這樣作就能夠了呢?請先看SingleLaunchActivityTestCase的Class Overview:api

SingleLaunchActivityTestCase
Class Overview
If you would like to test a single activity with an InstrumentationTestCase, this provides some of the boiler plate 
to launch and finish the activity in setUp() and tearDown(). This launches the activity only once for the entire 
class instead of doing it in every setup / teardown call.app

大概意思就是說若是測試用例是繼承自SingleLaunchActivityTestCase的話,setTup()和tearDown()會只運行一次而不是像ActivityInstrumentationTestCase2每調用一次函數都會運行一次。而這不恰好解決了咱們的問題了嗎!ide

代碼以下:函數

 

[java]  view plaincopy
  1. package com.example.android.notepad.test;  
  2.   
  3. import com.robotium.solo.Solo;  
  4.   
  5. import android.test.ActivityInstrumentationTestCase2;  
  6. import android.test.SingleLaunchActivityTestCase;  
  7. import android.app.Activity;  
  8.   
  9. @SuppressWarnings("rawtypes")  
  10. public class TCCreateNote2 extends SingleLaunchActivityTestCase{  
  11. //public class TCCreateNote2 extends ActivityInstrumentationTestCase2{  
  12.   
  13.     private static Solo solo = null;  
  14.     public Activity activity;  
  15.       
  16.     private static final int NUMBER_TOTAL_CASES = 2;  
  17.     private static int run = 0;  
  18.       
  19.     private static Class<?> launchActivityClass;  
  20.   
  21.     //對應re-sign.jar生成出來的信息框裏的兩個值  
  22.     private static String mainActiviy = "com.example.android.notepad.NotesList";  
  23.     private static String packageName = "com.example.android.notepad";  
  24.   
  25.     static {  
  26.   
  27.         try {  
  28.   
  29.             launchActivityClass = Class.forName(mainActiviy);  
  30.   
  31.         } catch (ClassNotFoundException e) {  
  32.   
  33.             throw new RuntimeException(e);  
  34.   
  35.         }  
  36.   
  37.     }  
  38.       
  39.       
  40.     @SuppressWarnings("unchecked")  
  41.     public TCCreateNote2() {  
  42.         super(packageName, launchActivityClass);  
  43.     }  
  44.   
  45.       
  46.     @Override  
  47.     public void setUp() throws Exception {  
  48.         //setUp() is run before a test case is started.   
  49.         //This is where the solo object is created.  
  50.         super.setUp();   
  51.         //The variable solo has to be static, since every time after a case's finished, this class TCCreateNote would be re-instantiated  
  52.         // which would lead to soto to re-instantiated to be null if it's not set as static  
  53.         //if(solo == null) {  
  54.         TCCreateNote2.solo = new Solo(getInstrumentation(), getActivity());  
  55.         //}  
  56.     }  
  57.       
  58.     @Override  
  59.     public void tearDown() throws Exception {  
  60.         //Check whether it's the last case executed.  
  61.         run += countTestCases();  
  62.         if(run >= NUMBER_TOTAL_CASES) {  
  63.             solo.finishOpenedActivities();  
  64.         }  
  65.     }  
  66.   
  67.     public void testAddNoteCNTitle() throws Exception {  
  68.           
  69.         solo.clickOnMenuItem("Add note");  
  70.         solo.enterText(0, "中文標籤筆記");  
  71.         solo.clickOnMenuItem("Save");  
  72.         solo.clickInList(0);  
  73.         solo.clearEditText(0);  
  74.         solo.enterText(0, "Text 1");  
  75.         solo.assertCurrentActivity("Expected EditTest Activity", "NoteEditor");  
  76.         solo.clickOnMenuItem("Save");  
  77.         solo.assertCurrentActivity("Expected NotesList Activity", "NotesList");  
  78.           
  79.         solo.clickLongOnText("中文標籤筆記");  
  80.         solo.clickOnText("Delete");  
  81.     }  
  82.       
  83.       
  84.     public void testAddNoteEngTitle() throws Exception {  
  85.         solo.clickOnMenuItem("Add note");  
  86.         solo.enterText(0, "English Title Note");  
  87.         solo.clickOnMenuItem("Save");  
  88.         solo.clickInList(0);  
  89.         solo.assertCurrentActivity("Expected EditTest Activity", "NoteEditor");  
  90.         solo.clearEditText(0);  
  91.         solo.enterText(0, "Text 1");  
  92.         solo.clickOnMenuItem("Save");  
  93.         solo.assertCurrentActivity("Expected NotesList Activity", "NotesList");  
  94.           
  95.         solo.clickLongOnText("English Title Note");  
  96.         solo.clickOnText("Delete");
相關文章
相關標籤/搜索