In this article we are going to create an Android Testing Application to test our HelloWorld application discussed here using Android Testing Framework.
The testcase for the HelloWorld application will be created using the class ActivityUnitTestCase which is available in the Android Testing Framework.
This Android testing application is developed in Eclipse ( 4.2.1 ) with ADT plugin ( 20.0.3 ) and Android SDK (R20.0.3).
1. What to Test
In this application, we will test, whether the text displayed in the HelloWorld application is “Helloworld!” or not. If ”Helloworld!” is displayed, then the testcase returns success other wise it returns failure.
2 Download HelloWorld Application
You can download the HelloWorld application from the given below link, which we are going to test in this article.

3. Extract the downloaded file and Import into Eclipse IDE
4. Create a new Android Testing application project
5. Enter Project Name
6. Select Target project to be tested
7. Select Build Target for this project
8. Create a testcase class in the file src/in/wptrafficanalyzer/helloworld/test/MainActivityTest.java with the given below code :
package in.wptrafficanalyzer.helloworld.test; import in.wptrafficanalyzer.helloworld.MainActivity; import android.content.Intent; import android.test.ActivityUnitTestCase; import android.test.suitebuilder.annotation.SmallTest; import android.widget.TextView; public class MainActivityTest extends ActivityUnitTestCase<MainActivity>{ // Activity of the Target application MainActivity mainActivity; // TextView of the MainActivity to be tested TextView tvHello; public MainActivityTest() { super(MainActivity.class); } @Override protected void setUp() throws Exception { super.setUp(); // Starts the MainActivity of the target application startActivity(new Intent(getInstrumentation().getTargetContext(), MainActivity.class), null, null); // Getting a reference to the MainActivity of the target application mainActivity = (MainActivity)getActivity(); // Getting a reference to the TextView of the MainActivity of the target application tvHello = (TextView) mainActivity.findViewById(in.wptrafficanalyzer.helloworld.R.id.hello_world); } @Override protected void tearDown() throws Exception { super.tearDown(); } @SmallTest public void testHello(){ // The actual text displayed in the textview String actual=tvHello.getText().toString(); // The expected text to be displayed in the textview String expected = "Hello world!"; // Check whether both are equal, otherwise test fails assertEquals(expected,actual ); } }
9. Running the TestCase
For running the testcase created in the above step, click Run->RunConfigurations menu and create a new configuration for the Android JUnit Test as shown in the given below screen and click Run button
10. Test Result
11. Download

12. Reference
http://developer.android.com/guide/index.html

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in
You can hire me on hourly basis or on project basis for Android applications development.
For hiring me, please mail your requirements to info@wptrafficanalyzer.in.
My other blogs
store4js.blogspot.com
Very Basic, good to start but i was expecting some more! like activity button click etc.
pls put sample code for unit testing using button click…
pls send sample code for unit testing using button click…
error for me please help me
the error is u need to use theme appcompact theme