" Jai Saraswati Maa"
Hi ... Dear All ..Today I am uploading a superb example of Android Instrumentation Test.
It will l test your code with your emulator or physical device.
In MainActivity i am inserting simple text in edit text and on button click i will display text inserted into TextView.
Its also known as Activity Test.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box.
Just Read Carefully ........ There is some configuration tips required.
**** Nothing added in manifest file or build gradle file so don't worry its very easy.
********* See when you run this test your application runs and disappear.
A. Layout will be like ....
B. xml code activity_main.xml
C. Java code MainActivity.java is as
Hi ... Dear All ..Today I am uploading a superb example of Android Instrumentation Test.
It will l test your code with your emulator or physical device.
In MainActivity i am inserting simple text in edit text and on button click i will display text inserted into TextView.
Its also known as Activity Test.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box.
Just Read Carefully ........ There is some configuration tips required.
**** Nothing added in manifest file or build gradle file so don't worry its very easy.
********* See when you run this test your application runs and disappear.
A. Layout will be like ....
B. xml code activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.exam.ravi.junittestex1.MainActivity"> <EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" /> <Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:id="@+id/button" android:layout_below="@+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:onClick="clickButton" /> <TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" /> </RelativeLayout>
C. Java code MainActivity.java is as
package com.exam.ravi.junittestex1; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void clickButton(View view) { EditText editText = (EditText) findViewById(R.id.editText); String name = editText.getText().toString(); String setData = String.format("Hello, %s!", name); TextView textView = (TextView) findViewById(R.id.textView); textView.setText(setData); } }
************* Now Test Procedure started .....
1. Create a Java Class under androidTest Package to test your MainActivity
****** i had created MainActTest which is as
package com.exam.ravi.junittestex1; import android.test.ActivityInstrumentationTestCase2; import android.test.TouchUtils; import android.widget.Button; import android.widget.TextView; public class MainActTest extends ActivityInstrumentationTestCase2<MainActivity> { public MainActTest() { super(MainActivity.class); } public void testActivityExists() { MainActivity activity = getActivity(); assertNotNull(activity); } public void testApply() { MainActivity activity = getActivity(); getInstrumentation().waitForIdleSync(); getInstrumentation().sendStringSync("Ravi"); Button button = (Button) activity.findViewById(R.id.button); TouchUtils.clickView(this, button); TextView message = (TextView) activity.findViewById(R.id.textView); String actualText = message.getText().toString(); assertEquals("Hello, Ravi!", actualText); }
}
2. How configure ......
step 1. Select Android Instrumentation Tests in Build Variant as
step 2. configure your AndroidTest Package using Edit Configuration with Run
step 3 Run your Test
3.1 Successful ..... when inserted and expected are same Text ....
3.2 Failed .... when inserted and expected are not same Text ....
No comments:
Post a Comment