在上一遍笔记博客中本觉得只能在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
代码以下:函数
- 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{
-
- 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;
-
-
- 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 {
-
-
- super.setUp();
-
-
-
- TCCreateNote2.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.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");