Being Tester we are used to reporting bugs and we know that developer debug the code to find out the line of code causing the bug. But now you are the one coding the test cases and you are responsible for solving any bug in your testing framework.
Android Studio provides some shortcut keys for debugging. In this post, we will use our HelloWord Espresso test Case Example and We will run our test Case In Debug mode and we will debug our code line by line.
It is always good practice to use Debug Mode for root tracing the bug as it allows you to run your code line by line by setting a debug point. Debug point is the nothing but marking the line of code from which you wish to start debugging. This speeds up your work and efficiency.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
package com.example.globalsqa.myapplication1; import android.app.Activity; import android.app.Instrumentation; import android.app.LauncherActivity; import android.content.Intent; import android.support.test.InstrumentationRegistry; import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; import android.support.test.runner.AndroidJUnitRunner; import android.test.ActivityInstrumentationTestCase2; import android.test.ActivityUnitTestCase; import android.test.InstrumentationTestRunner; import android.test.suitebuilder.annotation.SmallTest; import android.view.ContextThemeWrapper; import android.widget.TextView; import com.example.globalsqa.myapplication1.MainActivity; import com.robotium.solo.Solo; import org.junit.*; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; public class MyActivityTest extends ActivityInstrumentationTestCase2<Activity> { private static final String LAUNCHER_ACTIVITY_CLASSNAME = "com.example.globalsqa.myapplication1.MainActivity"; private static Class<?> launchActivityClass; static { try { launchActivityClass = Class.forName(LAUNCHER_ACTIVITY_CLASSNAME); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } private Solo solo; public MyActivityTest() { super((Class<Activity>) launchActivityClass); } @Override public void setUp() throws Exception { super.setUp(); solo = new Solo(getInstrumentation(), getActivity()); } @Test public void testText() throws Exception { // s=new Solo(this.getInstrumentation(),getActivity()); solo.waitForView("MainActivity"); Thread.sleep(3000); TextView t = (TextView) solo.getCurrentActivity().findViewById(R.id.globalsqa); assertEquals("Hello world!", t.getText().toString()); } public void tearDown() throws Exception { solo.finishOpenedActivities(); super.tearDown(); } } |
This will help you debug your test cases and write a efficient and bug free code.
Leave a Reply