Android provides several test classes above that allow us to test multiple objects, such as individual methods, Activity, Service, application, and so on, unit testing can be very convenient for us to test the code, and to facilitate the inspection of the reconstructed code. This article will briefly explain how to test the objects in Android.
First, the preparatory work
Ii. testing methods unrelated to the Android operating environment
2.1 Instrumentationtestcase
When you want to test methods unrelated to the Android environment, it is recommended to inherit instrumentationtestcase to test. For example, the following method of size is suitable for such a test.
Public Static int getmax (intint b) { return a >= b? a:b; }
The code that gets the version number is related to the context of the Android run, we have to pass in a context object, and there is no way to test the inheritance instrumentationtestcase.
/**get the version number of the current app *@paramContext *@return */ Public StaticString Getversionname (context context) {Try{PackageInfo Manager= Context.getpackagemanager (). Getpackageinfo (Context.getpackagename (), 0); returnManager.versionname; } Catch(namenotfoundexception e) {return"Unknown"; } }
So isn't it possible to test things like activity with it? No, we can still use it to test activity, but the question is to initialize the object through code, but since its subclasses can be well tested for activity, we don't normally use it for testing activity. In the second section, a simple demo is given, and the demo that tests the activity with it is given.
2.2 Examples
The target class for the test is--myutils:
package Com.kale.androidtest; import Android.content.Context; import Android.content.pm.PackageInfo; import android.content.pm.PackageManager.NameNotFoundException; public class Myutils { static int Getmax (int A, int b) { return a >= b? A:B; }}
Test class--mysimpletest:
Package com.kale.androidtest.test; Import com.kale.androidtest.MyUtils; Import android.test.InstrumentationTestCase; Public class extends Instrumentationtestcase { publicvoid Testgetmax () { int max = Myutils.getmax (1, 3 ); Assertequals (3, max);} }
Attached: Testing activity
The activity code is not posted, there is a edittext, the test here is a simple example, that it can be used to test the activity, the example does not have any practical significance.
Packagecom.kale.androidtest.test;Importandroid.content.Intent;ImportAndroid.os.SystemClock;Importandroid.test.InstrumentationTestCase;ImportAndroid.widget.EditText;Importcom.kale.androidtest.MyActivity;/** * @author: Jack Tony * @description: * @web:http://www.oschina.net/question/54100_27061* @date: February 19, 2015*/ Public classMySampleTest2extendsinstrumentationtestcase {myactivity mactivity; EditText Medittext; @Overrideprotected voidSetUp ()throwsException {//start an activity with intentIntent Intent =NewIntent (); Intent.setclassname ("Com.kale.androidtest", myactivity.class. GetName ()); Intent.setflags (Intent.flag_activity_new_task); Mactivity=(myactivity) getinstrumentation (). Startactivitysync (Intent); } /*** @description Test initialization complete **/ Public voidTestinit () {Medittext=Mactivity.getedittext (); Assertnotnull (mactivity); Assertnotnull (Medittext); } /*** @description Test to get the text in EditText in activity **/ Public voidTestgettext () {Medittext=Mactivity.getedittext (); String text=Medittext.gettext (). toString (); Assertequals ("", text); } /*** @description Test how to set text **/ Public voidTestsettext () {Medittext=Mactivity.getedittext (); //setting text in the main threadGetinstrumentation (). Runonmainsync (NewRunnable () {@Override Public voidrun () {Medittext.settext ("Kale"); } }); //Pause 1500msSystemclock.sleep (1000); Assertequals ("Kale", Medittext.gettext (). toString ()); } /*** Garbage Cleanup and resource recovery * *@seeAndroid.test.instrumentationtestcase#teardown ()*/@Overrideprotected voidTearDown () {mactivity.finish (); Try { Super. TearDown (); } Catch(Exception e) {e.printstacktrace (); } }}
Third, test application
Android Unit Test