Robotium编写测试用例如何模拟Junit4的BeforeClass和AfterClass方法1

Robotium的测试类ActivityInstrumentationTestCase2是继承于Junit3的TestCase类,所以并没有提供Junit4的特性.如网上总结说的

  • 不能通过annotate的方式来识别子类的新特征,如不能实现@beforeclass,@afterclass等特征。只能通过写setup和teardown,
  • TestCase只能以test开头进行测试case书写。
那么有时我们并不想每次开始/完成一个case的时候都做一些重复的动作,也就是要实现Junit4的@beforeclass和@afterclass,该怎么办呢?
以SDK自带的Notepad测试用例作为例子,假如现在我们需要实现两个测试用例
  • testAddNoteCNTittle:创建一个中文标题的笔记
  • testAddNoteEngTitle:创建一个英文标题的笔记
根据实例提供的代码,在setup里面会初始化solo而在teardown里面会关闭所有打开的activities,也就是说每执行一个case都会重新初始化一次solo和关闭所有的activities:
  1. @Override  
  2. public void setUp() throws Exception {  
  3.     //setUp() is run before a test case is started.    
  4.     //This is where the solo object is created.   
  5.     super.setUp();  
  6.   
  7.     this.activity = this.getActivity();  
  8.   
  9.     this.solo = new Solo(getInstrumentation(), getActivity());  
  10. }  
  11.   
  12. @Override  
  13. public void tearDown() throws Exception {  
  14.     //tearDown() is run after a test case has finished.    
  15.     //finishOpenedActivities() will finish all the activities that have been opened during the test execution.   
  16.     solo.finishOpenedActivities();  
  17. }  
	@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();
	}
但事实上我们在这个脚本只是去创建两个Note,并不需要每执行完一个case都要去初始化solo和关闭所有activities。google后没有发现有现成的取代@beforeclass和@aferclass的方法。
以下本人的实现方法
  1. <pre name="code" class="java">package com.example.android.notepad.test;  
  2.   
  3. import com.robotium.solo.Solo;  
  4.   
  5. import android.test.ActivityInstrumentationTestCase2;  
  6. import android.app.Activity;  
  7.   
  8. @SuppressWarnings("rawtypes")  
  9. public class TCCreateNote extends ActivityInstrumentationTestCase2{  
  10.   
  11.     private static Solo solo = null;  
  12.     public Activity activity;  
  13.       
  14. <span style="white-space:pre">    </span>private static final int NUMBER_TOTAL_CASES = 2;  
  15.     private static int run = 0;  
  16.       
  17.     private static Class<?> launchActivityClass;  
  18.   
  19.     //对应re-sign.jar生成出来的信息框里的两个值   
  20.     private static String mainActiviy = "com.example.android.notepad.NotesList";  
  21.     private static String packageName = "com.example.android.notepad";  
  22.   
  23.     static {  
  24.   
  25.         try {  
  26.   
  27.             launchActivityClass = Class.forName(mainActiviy);  
  28.   
  29.         } catch (ClassNotFoundException e) {  
  30.   
  31.             throw new RuntimeException(e);  
  32.   
  33.         }  
  34.   
  35.     }  
  36.       
  37.       
  38.     @SuppressWarnings("unchecked")  
  39.     public TCCreateNote() {  
  40.         super(packageName, launchActivityClass);  
  41.     }  
  42.   
  43.       
  44.     @Override  
  45.     public void setUp() throws Exception {  
  46.         //setUp() is run before a test case is started.    
  47.         //This is where the solo object is created.   
  48.         super.setUp();   
  49. <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   
  50.         // which would lead to soto to re-instantiated to be null if it's not set as static   
  51.         if(solo == null) {  
  52.             TCCreateNote.solo = new Solo(getInstrumentation(), getActivity());  
  53.         }  
  54.     }  
  55.       
  56.     @Override  
  57.     public void tearDown() throws Exception {  
  58.         //Check whether it's the last case executed.   
  59.         run += countTestCases();  
  60.         if(run >= NUMBER_TOTAL_CASES) {  
  61.             solo.finishOpenedActivities();  
  62.         }  
  63.     }  
  64.   
  65.     public void testAddNoteCNTitle() throws Exception {  
  66.           
  67.         solo.clickOnMenuItem("Add note");  
  68.         solo.enterText(0, "中文标签笔记");  
  69.         solo.clickOnMenuItem("Save");  
  70.         solo.clickInList(0);  
  71.         solo.clearEditText(0);  
  72.         solo.enterText(0, "Text 1");  
  73.         solo.clickOnMenuItem("Save");  
  74.         solo.assertCurrentActivity("Expected NotesList Activity", "NotesList");  
  75.           
  76.         solo.clickLongOnText("中文标签笔记");  
  77.         solo.clickOnText("Delete");  
  78.     }  
  79.       
  80.       
  81.     public void testAddNoteEngTitle() throws Exception {  
  82.         solo.clickOnMenuItem("Add note");  
  83.         solo.enterText(0, "English Title Note");  
  84.         solo.clickOnMenuItem("Save");  
  85.         solo.clickInList(0);  
  86.         solo.clearEditText(0);  
  87.         solo.enterText(0, "Text 1");  
  88.         solo.clickOnMenuItem("Save");  
  89.         solo.assertCurrentActivity("Expected NotesList Activity", "NotesList");  
  90.           
  91.         solo.clickLongOnText("English Title Note");  
  92.         solo.clickOnText("Delete");  
  93.     }  
  94. }  
原文地址:https://www.cnblogs.com/TestingOn/p/3980942.html