Suppose you have implemented a compute percent function that requires testing, code:
public class Progressservice {public Integer Getcurrentprogerss (double current, double max) { Integer i= (int) (( Current/max); return i; }}
To unit test a specified feature, the following steps are detailed:
A test class
Writing a test class to perform our function, this class needs to inherit Androidtestcase.
Import Android.test.androidtestcase;import Android.util.log;import Com.example.service.progressservice;public Class Progressservicejunit extends Androidtestcase { private final String tag= "main"; Public Progressservicejunit () { //TODO auto-generated constructor stubs } public void Getcurrentprogersstest () { progressservice progressservice=new progressservice (); Integer Pro=progressservice.getcurrentprogerss (+); LOG.I (TAG, pro.tostring ());} }
Two JUnit supports the addition of JUnit support in Androidmanifest.xml and the development of a test project package.
1 <application> node before joining:
<uses-permission android:name= "Android.permission.RUN_INSTRUMENTATION"/> Add permission <instrumentationandroid: Name= "Android.test.InstrumentationTestRunner" fixed class capacity android:targetpackage= "Com.example.junittestdemo" > To test the package where the feature is located </instrumentation>
2 manifest.xml <application> added:
<application>...<uses-library android:name= "Android.test.runner"/> ...<application/>
Three runs the left mouse button on the test case method, run As→android JUnit test.
The Eclipse unit test for Android Development Basics