Robotium的測試類ActivityInstrumentationTestCase2是繼承於Junit3的TestCase類,因此並無提供Junit4的特性.如網上總結說的java
- 不能經過annotate的方式來識別子類的新特徵,如不能實現@beforeclass,@afterclass等特徵。只能經過寫setup和teardown,
- TestCase只能以test開頭進行測試case書寫。
那麼有時咱們並不想每次開始/完成一個case的時候都作一些重複的動做,也就是要實現Junit4的@beforeclass和@afterclass,該怎麼辦呢?
以SDK自帶的Notepad測試用例做爲例子,假如如今咱們須要實現兩個測試用例
- testAddNoteCNTittle:建立一箇中文標題的筆記
- testAddNoteEngTitle:建立一個英文標題的筆記
根據實例提供的代碼,在setup裏面會初始化solo而在teardown裏面會關閉全部打開的activities,也就是說每執行一個case都會從新初始化一次solo和關閉全部的activities:
- @Override
- public void setUp() throws Exception {
-
-
- super.setUp();
-
- this.activity = this.getActivity();
-
- this.solo = new Solo(getInstrumentation(), getActivity());
- }
-
- @Override
- public void tearDown() throws Exception {
-
-
- solo.finishOpenedActivities();
- }
@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的方法。
如下本人的實現方法
- <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;
-
-
- 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 {
-
-
- super.setUp();
- <span style="white-space:pre"> </span>
-
- if(solo == null) {
- TCCreateNote.solo = new Solo(getInstrumentation(), getActivity());
- }
- }
-
- @Override
- public void tearDown() throws Exception {
-
- run += countTestCases();
- if(run >= NUMBER_TOTAL_CASES) {
- solo.finishOpenedActivities();
- }
- }
-
- public void testAddNoteCNTitle() throws Exception {
-
- solo.clickOnMenuItem("Add note");
- solo.enterText(0, "中文標籤筆記");
- 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("中文標籤筆記");
- 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");
- }
- }