unit testing - Android Test : ActivityInstrumentationTestCase2 : Test 2 buttons, where each one navigates to another activity -
i trying make android tests application. have activity (a) contains 2 buttons, each 1 navigating activity (b) different (data) in intent.
i able test 1 of them, not both. because when perform click of first, navigate next activity (b) , second button not visible anymore.
my question : 1- possible make many case of tests in same activity ? 2- there way or best practice creat many cases scenarios test ?
example ---> click in first button , navigate activity (b) , restart activity (a) again, , click in second button ?
thanks anyway.
this code of activitytest :
public class myactivitytest extends activityinstrumentationtestcase2<myactivity> { /** * first button. */ view abutton; /** * second button. */ view bbutton; /** * activity under test. */ myactivity activity; /** * create new myactivitytest. */ public myactivitytest() { super(myactivity.class); } /* * (non-javadoc) * * @see android.test.activityinstrumentationtestcase2#setup() */ @override protected void setup() throws exception { super.setup(); activity = getactivity(); } /** * tests behavior when user selects first button. */ @mediumtest public void testclickfirstbutton() { // ensure valid handle activity has been returned assertnotnull(activity); abutton = (view) activity .findviewbyid(mypackage.r.id.first_button); assertnotnull(abutton); activity.runonuithread(new runnable() { public void run() { abutton.performclick(); } }); // wait request go through getinstrumentation().waitforidlesync(); } /** * ========================================> test not running * tests behavior when user selects second button. */ @mediumtest public void testclicksecondbutton() { // ensure valid handle activity has been returned assertnotnull(activity); bbutton = (view) activity .findviewbyid(mypackage.r.id.second_button); assertnotnull(bbutton); activity.runonuithread(new runnable() { public void run() { bbutton.performclick(); } }); // wait request go through getinstrumentation().waitforidlesync(); } }
i found solution, if have better 1 welcome:
i update code overriding teardown()
, , using instance of solo , move case closing opened activities, updated setup() method
@override protected void setup() throws exception { activity = getactivity(); solo = new solo(getinstrumentation(), getactivity()); } @override public void teardown() throws exception { // teardown() run after test case has finished. // finishopenedactivities() finish activities have // been opened during test execution. solo.finishopenedactivities(); super.teardown(); }
so test cases running 1 one . activity closed , restarted each case.
from solo coming?
you need add robotium dependency:
dependencies { // unit testing dependencies androidtestcompile 'com.jayway.android.robotium:robotium:5.4.12' }
you can have on this tutorial.
Comments
Post a Comment