Introduction
If you have an Android app project , You can follow these steps to generate the Code Coverage informatin out of your Unit Testing.
If you have an Android app project , You can follow these steps to generate the Code Coverage informatin out of your Unit Testing.
Assumptions
You have a "standard" Android app project in Eclipse. For this document, let's assume the app project is called "luniks-app" and the test project "luniks-test", and that your Android SDK is installed in "/data/android-sdk".
This document also assumes that you are checking in your projects to a version control system like Subversion, and that you are checking them out in Jenkins from there.
You have a "standard" Android app project in Eclipse. For this document, let's assume the app project is called "luniks-app" and the test project "luniks-test", and that your Android SDK is installed in "/data/android-sdk".
This document also assumes that you are checking in your projects to a version control system like Subversion, and that you are checking them out in Jenkins from there.
Create Test Project
File->New->Project->Android Test Project ->
File->New->Project->Android Test Project ->
Create JUnit TestCase
File->New->JUnit TestCase. Make sure that the TrstActivity is derieved from ActivityInstrumentationTestCase2 class like this.
File->New->JUnit TestCase. Make sure that the TrstActivity is derieved from ActivityInstrumentationTestCase2 class like this.
public class Test extends
ActivityInstrumentationTestCase2<hellogod> {
private
hellogod mActivity; // the activity
under test
private
TextView mView; // the
activity's TextView (the only view)
private
String resourceString;
public
Test() {
super("indianic.hellogod.hellogod",
hellogod.class);
}
@Override
protected
void setUp() throws Exception {
super.setUp();
mActivity
= this.getActivity();
Thread.sleep(300000);
int
y=7;
// mView = (TextView)
mActivity.findViewById(com.example.helloandroid.R.id.textview);
// resourceString =
mActivity.getString(com.example.helloandroid.R.string.hello);
}
public void
testPreconditions() {
//
assertNotNull(mView);
}
public void
testText() {
//
assertEquals(resourceString,(String)mView.getText());
}
}
Above code will run for the specific amount of time which
we have passes in argument of sleep method.
But we want something which keep listening whether application is running or not..
Here is the code snippet for that...
But we want something which keep listening whether application is running or not..
Here is the code snippet for that...
@Override
protected
void setUp() throws Exception {
super.setUp();
mActivity
= this.getActivity();
Context
context = mActivity.getApplication().getApplicationContext();
ActivityManager
mgr = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
while(flag)
{
String
text = "";
List<RunningTaskInfo>
tasks = mgr.getRunningTasks(100);
int
processCount;
for(processCount=0;processCount<tasks.size();processCount++)
{
RunningTaskInfo p = (RunningTaskInfo)tasks.get(processCount);
String packageName=p.baseActivity.getPackageName();
if(packageName.equals("indianic.hellogod.hellogod"))
{
//This means our application is still running.
break;
}
}
if(processCount<tasks.size())
{
Log.v(TAG,"Sleep for 10 seconds");
//Take a pause for 10 seconds
Thread.sleep(10000);
}
else
{
Log.v(TAG,"Exiting from Test Case");
//App is not running and we need to stop the test case..
flag=false;
}
}
}
The above loop will check whether app is running or not by checking process table using ActivityManager class.
Set up Ant
Install Ant if you don't have it already.
Now open terminal and go to the parent folder of the project folder.run the following command to set up Ant for the app project:
Install Ant if you don't have it already.
Now open terminal and go to the parent folder of the project folder.run the following command to set up Ant for the app project:
android update project
-p luniks-app
Then run this command to set up Ant for the test project. Note that the path given to -m must be the relative path to the project under test, seen from the test project, not the workspace:
android update
test-project -m ../luniks-app -p luniks-test
Refresh the projects in Eclipse
Note: The file "local.properties" contains local configuration, like the location where your Android SDK is installed. Since this location can be different on i.e. Jenkins build machines, this file should not be checked in to version control. So I recommend to add "local.properties" to the "Ignored Resources" in Eclipse right away.
Test if you can build the app project with
Note: The file "local.properties" contains local configuration, like the location where your Android SDK is installed. Since this location can be different on i.e. Jenkins build machines, this file should not be checked in to version control. So I recommend to add "local.properties" to the "Ignored Resources" in Eclipse right away.
Test if you can build the app project with
ant clean compile
Start the Emulator
Make sure that you have a (virtual) device (only one at a time!) running, and see if you can build the test project and run the tests:
launch emulator from outside like Eclipse->window->"Android SDK and AVD Manager" -> Select the device and click Start
ant clean coverage
This will take a bit longer. Eventually, you should find a "coverage.html" in a "coverage" subfolder in your test project, showing the test coverage.
You might want to add the "coverage" folder to the version control "Ignored Resources" as well.
To avoid the tedious process find an attached shell
script. Put this script in your work space directory. Pass the name of your app
project name and test project name in parameter.
As an output you will find the coverage folder in your test project.
As an output you will find the coverage folder in your test project.
Additional Information
Somehow I have not varified how this can be done without creating testcase? whether a time Delay i robbust enough for covering all scenario etc.
Expecting Android team to explore this tool further and come up with better method. for our coverage process..
Somehow I have not varified how this can be done without creating testcase? whether a time Delay i robbust enough for covering all scenario etc.
Expecting Android team to explore this tool further and come up with better method. for our coverage process..
Some links
https://wiki.jenkins-ci.org/display/JENKINS/Building+an+Android+app+and+test+project
No comments:
Post a Comment