Thursday, March 21, 2013

Android Unit Testing




What to test?


We should test every statement in ourcode but this also depends on different criteria and can be reduced to test the path of execution or just some methods. Usually there is no need to test something that can't be broken, for example it usually makes no sense to test getters and setters as you probably won't be testing the Java compiler on your own code and the compiler would have already performed its own tests. In addition to the functional areas you should test, there are some specifi c areas of Android applications that you should consider. We will be looking at these in the following sections.

1 Activity lifecycle events
2 Database and fi lesystem operations
3 Physical characteristics of the device

- Network capabilities
- Screen densities
- Screen resolutions
-  Screen sizes
- Availability of sensors
- Keyboard and other input devices
- GPS
- External storage

4 UI tests
5 Integration tests

Andorid JUnit Testing


Installation:
Install Eclipse ADT plug in from here. It includes Android Project and Android Test Project.
Some common Test cases:
TestCase – It is a JUnit test case. It can be extended to test utility classes that are not tied to 
the Android framework.
AndroidTestCase – It extends JUnit’s TestCase. It doesn’t need to launch an activity to run it. It 
can be used when user wants to access Resources that depend on Activity Context. 
ActivityInstrumentationTestCase2<T extends android.app.Activity> – It provides UI and 
functional testing for a single activity.
ActivityUnitTestCase<T extends android.app.Activity> – It gives the tested activity an isolated 
environment. The activity under test will be created with minimal connection to the system 
infrastructure.
ApplicationTestCase<T extends android.app.Application> – It provides testing for Application 
classes. It can be used to test the life cycle of an application.
Example on ActivityInstrumentationTestCase2:
Let’s perform a GUI and functionality test on the below login screen. 


The Activity for above screen is “ScanBarcode”. It is present under package “com.uta.project.scanbarcode”.
Steps for creating Android JUnit Project
1) File -> New -> Project
2) Under Android, select Android Test Project and press Next >
3) You should be able to see below screen


4) Browse under “An existing Android project” and select the Project to be tested.


It will populate all the required fields.
5) Give a project name. Click on “Finish”.
A page will be generated but with errors.
6) So to remove errors give the valid activity name under test.
Like in this example, activity is “ScanBarcode”. Hence modified the code as
public class LoginTestActivity extends ActivityInstrumentationTestCase2<ScanBarcode>
Also give valid parameters to function super() under constructor.
public LoginActivity()
{
super("com.uta.project.scanbarcode", ScanBarcode.class);
}
Now the Junit test case is ready for UI and functionality testing. Happy Coding!!!










No comments:

Post a Comment