Wednesday, 20 January 2016

How to Add and Run Your First Espresso Test Case ?



Before adding any test case it is important that you follow a proper project structure. Click here to know where to add your Espresso instrumented test cases.

Video Tutorial -



By the end of this post you will be ready with your first Espresso test case. There are two ways to write test case suit in Espresso and this post will cover both of them for simple Hello World App.

First way is using Activity Instrumentation.Below is Simple test case which checks the UI Element with given ID is displayed after launching activity ->
1. The class uses two annotations @LargeTest and @Runwith these two annotations are used to specify the behavior for our TestClass.
2. The class extends AcitivtyInstrumentationTestCase2 class with launch activity as MainActivity which we want to test in this example.
3. @Before and @After is annotations and used to specify which method should be executed before each test case and which method should be executed after completion of each test case.
4. @Test annotation is added for each test case method.
5. In the below test case the getAcitivity() launches our Main Activity.
6. onView() is used to select a view for testing and withId() is used to locate the UI element and rest is used to check whether the particular element is displayed or not.



@RunWith(AndroidJUnit4.class)
@LargeTest
public class EspressoTest1 extends ActivityInstrumentationTestCase2<MainActivity>{

        public EspressoTest1() {
            super(MainActivity.class);
        }

        @Before 
         public void setUp() throws Exception {
            super.setUp();
            injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        }

        @Test 
        public void test1ChatId() {
            getActivity();
            onView(withId(R.id.anuja)).check(matches(isDisplayed()));
        }

        @After        public void tearDown() throws Exception {
            super.tearDown();
        }
}


Second way is without using Activity Instrumentation ->
1. The class uses two annotations @LargeTest and @Runwith these two annotations are used to specify the behavior for our TestClass.
2.The below line of code is used to run the test case in particular order.
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
3. @Before and @After is annotations and used to specify which method should be executed before each test case and which method should be executed after completion of each test case.
4.onView() is used to select a view for testing and withId() is used to locate the UI element and rest is used to check whether the particular element is displayed or not.


@RunWith(AndroidJUnit4.class)
@LargeTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class EspressoTest2 { 

@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
        MainActivity.class); 


 @Before 
 public void setUp() throws Exception {
        //Before Test case execution 
}

    @Test     
      public void test1ChatId() {
        onView(withId(R.id.anuja)).check(matches(isDisplayed()));
    }

    @After 
public void tearDown() throws Exception {
    //After Test case Execution
    }
}


How to run these test cases ->
1. Click on "Build Variants".
2. Select Android Instrumentation Tests.
First Espresso Test
First Espresso Test

3. Select class name or test case name -> right click -> Run
Write & Run Espresso Test
Running First Espresso Test


Note - 
add below code the defaultConfig before running the test case -

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"


I hope this post helps you find your code coverage for your test suit :)
Please Share your feedback in comments section below and follow QA Automated to get latest post update.Happy Testing :-)

7 comments:

  1. Dont we need this in default config in build.gradle?
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    ReplyDelete
  2. Yes We need that you can do it in Edit run configuration as well while running test case

    ReplyDelete
  3. Hey Anuja,

    Can i know how to inspect Elements ?
    Actually i am very new to Espresso Automation tool

    ReplyDelete
  4. Hi,

    I am getting below error
    java.lang.Exception: No runnable methods
    at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
    at org.junit.runners.ParentRunner.(ParentRunner.java:84)
    at org.junit.runners.BlockJUnit4ClassRunner.(BlockJUnit4ClassRunner.java:65)

    ReplyDelete
    Replies
    1. I am facing the same issue. Any help on the same?

      Delete
  5. Hi i am new in Android app automation testing, we dont have application source code how can i test without source code of app i have APK file kindly suggest

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete