Unit Testing and Instrumentation Testing in Android
Posted By : Chandan Wadhwa | 08-Jun-2016
Testing is an important part of android application development cycle. Detecting errors in eary stage of developmet can help you development and give you confidence in your code. Using android studio you can ceate unit test and instrumentation test cases on a variety of physical or virtual Android devices.
Unit test cases are run on the local machine on virtual device or android device.
I this blog I am going to explain you about the unit test and instrumentation test using android studio. So first I am explaning you about the unit test.
Unit Test in Android Studio:-
Configure your project for Unit Test :-
In your app's top-level build.gradle file, you need to add these libraries as dependencies:
dependencies {
testCompile 'junit:junit:4.12'
}
Create a project structure like below :-
App -> src -> test -> java -> ExampleUnitTest
Add the following code in ExampleUnitTest.java :-
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}
Running your test case Right click on "ExampleUnitTest" -> Run ExampleUnitTest
Result will show you the passed or failed result based on input.
UI Testing using Espresso :-
Testing user interface testing will ensure you the user do not enounter any error or unexpected result when user interact with your application.
The Espresso testing framework, provided by the Android Testing Support Library, provides APIs for writing UI tests to simulate user interactions within app.
In your app's top-level build.gradle file, you need to add these libraries as dependencies:
androidTestCompile 'com.android.support:support-annotations:23.3.0' androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
Create expresso test class :- app -> src -> androidTest -> java -> Define instrument test class Add the following code in layout file :-
Add the following code in MainActivity class :-
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Submit(View v)
{
TextView txt=(TextView)findViewById(R.id.lbl);
EditText edittxt=(EditText)findViewById(R.id.edittext);
txt.setText("Hello, "+edittxt.getText().toString());
}
}
Add the following code in your test class :-
@RunWith(AndroidJUnit4.class)
public class MainActivityInstrumentationTest {
private static final String STRING_TO_BE_TYPED = "Peter";
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
MainActivity.class);
@Test
public void sayHello(){
onView(withId(R.id.edittext)).perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard()); //line 1
onView(withText("Submit")).perform(click()); //line 2
String expectedText = "Hello, " + STRING_TO_BE_TYPED;
onView(withId(R.id.lbl)).check(matches(withText(expectedText)));
}
}
Running your test case Right click on MainActivityInstrumentationTest -> Run MainActivityInstrumentationTest
THANKS
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Chandan Wadhwa
Chandan is an Android Apps developer with good experience in building native Android applications.