在上一遍筆記博客中本以為只能在Setup和TearDown中做條件判斷來實現Junit4的@BeforeClass和@AfterClass功能。今天查看SDK時發現其實是有現成的方法來實現這個功能的。
方法就是把編寫的測試用例從繼承自ActivityInstrumentationTestCase2改成繼承成SingleLaunchActivityTestCase。
為什么這樣做就可以了呢?請先看SingleLaunchActivityTestCase的Class Overview:
SingleLaunchActivityTestCaseClass OverviewIf 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每調用一次函數都會運行一次。而這不剛好解決了我們的問題了嗎!
代碼如下:
package com.example.android.notepad.test;
import com.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
import android.test.SingleLaunchActivityTestCase;
import android.app.Activity;
@SuppressWarnings("rawtypes")
public class TCCreateNote2 extends SingleLaunchActivityTestCase{
//public class TCCreateNote2 extends ActivityInstrumentationTestCase2{
private static Solo solo = null;
public Activity activity;
private static final int NUMBER_TOTAL_CASES = 2;
private static int run = 0;
private static Class<?> launchActivityClass;
//對應re-sign.jar生成出來的信息框里的兩個值
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 TCCreateNote2() {
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();
//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) {
TCCreateNote2.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, "中文標簽筆記");
solo.clickOnMenuItem("Save");
solo.clickInList(0);
solo.clearEditText(0);
solo.enterText(0, "Text 1");
solo.assertCurrentActivity("Expected EditTest Activity", "NoteEditor");
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.assertCurrentActivity("Expected EditTest Activity", "NoteEditor");
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");