本文来源于:http://blog.csdn.net/zhubaitian/article/details/39296753
在上一遍笔记博客中本以为只能在Setup和TearDown中做条件判断来实现Junit4的@BeforeClass和@AfterClass功能。今天查看SDK时发现其实是有现成的方法来实现这个功能的。
方法就是把编写的测试用例从继承自ActivityInstrumentationTestCase2改成继承成SingleLaunchActivityTestCase。
为什么这样做就可以了呢?请先看SingleLaunchActivityTestCase的Class Overview:
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.
大概意思就是说如果测试用例是继承自SingleLaunchActivityTestCase的话,setTup()和tearDown()会只运行一次而不是像ActivityInstrumentationTestCase2每调用一次函数都会运行一次。而这不刚好解决了我们的问题了吗!
代码如下:
1 package com.example.android.notepad.test; 2 3 import com.robotium.solo.Solo; 4 import android.test.ActivityInstrumentationTestCase2; 5 import android.test.SingleLaunchActivityTestCase; 6 import android.app.Activity; 7 8 @SuppressWarnings("rawtypes") 9 public class TCCreateNote2 extends SingleLaunchActivityTestCase{ 10 //public class TCCreateNote2 extends ActivityInstrumentationTestCase2{ 11 private static Solo solo = null; 12 public Activity activity; 13 private static final int NUMBER_TOTAL_CASES = 2; 14 private static int run = 0; 15 16 private static Class<?> launchActivityClass; 17 //对应re-sign.jar生成出来的信息框里的两个值 18 private static String mainActiviy = "com.example.android.notepad.NotesList"; 19 private static String packageName = "com.example.android.notepad"; 20 21 static { 22 try { 23 launchActivityClass = Class.forName(mainActiviy); 24 } catch (ClassNotFoundException e) { 25 throw new RuntimeException(e); 26 } 27 } 28 29 @SuppressWarnings("unchecked") 30 public TCCreateNote2() { 31 super(packageName, launchActivityClass); 32 } 33 34 @Override 35 public void setUp() throws Exception { 36 //setUp() is run before a test case is started. 37 //This is where the solo object is created. 38 super.setUp(); 39 //The variable solo has to be static, since every time after a case's finished, this class TCCreateNote would be re-instantiated 40 // which would lead to soto to re-instantiated to be null if it's not set as static 41 //if(solo == null) { 42 TCCreateNote2.solo = new Solo(getInstrumentation(), getActivity()); 43 //} 44 } 45 46 @Override 47 public void tearDown() throws Exception { 48 //Check whether it's the last case executed. 49 run += countTestCases(); 50 if(run >= NUMBER_TOTAL_CASES) { 51 solo.finishOpenedActivities(); 52 } 53 } 54 55 public void testAddNoteCNTitle() throws Exception { 56 solo.clickOnMenuItem("Add note"); 57 solo.enterText(0, "中文标签笔记"); 58 solo.clickOnMenuItem("Save"); 59 solo.clickInList(0); 60 solo.clearEditText(0); 61 solo.enterText(0, "Text 1"); 62 solo.assertCurrentActivity("Expected EditTest Activity", "NoteEditor"); 63 solo.clickOnMenuItem("Save"); 64 solo.assertCurrentActivity("Expected NotesList Activity", "NotesList"); 65 solo.clickLongOnText("中文标签笔记"); 66 solo.clickOnText("Delete"); 67 } 68 69 public void testAddNoteEngTitle() throws Exception { 70 solo.clickOnMenuItem("Add note"); 71 solo.enterText(0, "English Title Note"); 72 solo.clickOnMenuItem("Save"); 73 solo.clickInList(0); 74 solo.assertCurrentActivity("Expected EditTest Activity", "NoteEditor"); 75 solo.clearEditText(0); 76 solo.enterText(0, "Text 1"); 77 solo.clickOnMenuItem("Save"); 78 solo.assertCurrentActivity("Expected NotesList Activity", "NotesList"); 79 solo.clickLongOnText("English Title Note"); 80 solo.clickOnText("Delete"); 81 } 82 }