重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Robotium的测试类ActivityInstrumentationTestCase2是继承于Junit3的TestCase类,所以并没有提供Junit4的特性.如网上总结说的
成都创新互联-专业网站定制、快速模板网站建设、高性价比洪江管理区网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式洪江管理区网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖洪江管理区地区。费用合理售后完善,十余年实体公司更值得信赖。
@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的方法。
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; 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 TCCreateNote() { 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) { TCCreateNote.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.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"); } }