Andorid JUnit Testing

joyfulfightMobile - Wireless

Dec 10, 2013 (3 years and 11 months ago)

115 views

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 test
ed 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 tes
t 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.scanbarcod
e”.

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 p
age 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!!!

References:

http://developer.android.com/reference/android/test/AndroidTestCase.html

http://mihaifonoage.blogspot.com/2010/01/unit
-
and
-
functional
-
testing
-
in
-
android.html

http://docs.google.com/viewer?a=v&pid=sites&srcid=Y29kdGVjaC5jb218YW5kcm9pZHxne
Do1MjA5MGNjY2YwMTFjZDYz