[Android primary]android Unit test activity individually tested

Source: Internet
Author: User

There is a situation where we developers may have to maintain the completed application, one of the interface may be a bug, so in order to test a possible problem of the functional interface from the portal has been started to jump to the interface, this situation, our testers can keep skipping some non-test interface, wasting time. Here is a small way, is the Android system test plan, you can try, today I tried the activity of the individual test method, feel good, here to share to you.

Let's start by simulating a project we're going to be testing: Testactivity and the landing interface for the main test

The function to be tested is to simulate the user input, and the login operation, as well as the operation verification of the reset function. Okay, let's see what we can do next.

1. First look at the code in the Simulation project, on 2 simple files, a mock landing of the main interface, a landing after the jump interface.

Mainactivity.java

/** * Login Main Interface, demo demo * @author Jan */public class Mainactivity extends Activity implements onclicklistener{private static F inal String TAG = "mainactivity";p rivate EditText mloginname;private EditText mpwd;private Button mloginbutton;private Bu Tton Mrestbutton; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main); mloginname = (EditText) Findviewbyid (r.id.login_name_et); mpwd = (EditText) Findviewbyid (r.id.pwd_et); Mloginbutton = (Button) Findviewbyid (r.id.login); Mrestbutton = (Button) Findviewbyid ( R.id.reset); Mloginbutton.setonclicklistener (this), Mrestbutton.setonclicklistener (this);} @Overrideprotected void OnDestroy () {Super.ondestroy ();} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case r.id.login:login (); break;case R.id.reset:reset (); /** * Login, no actual verification and other operations, here is to jump to another interface */private void login () {Intent Intent = new Intent (Mainactivity.this, Resultactivity.class); Intent.putextra ("Name", Mloginname.gettexT (). ToString ()); Intent.putextra ("pwd", Mpwd.gettext (). ToString ()); startactivity (intent); /** * Reset, clear all input */private void Reset () {Mloginname.settext (""); Mpwd.settext ("");}}
Resultactivity.java

public class Resultactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.result); Intent get = Getintent (); String name = Get==null? "": Get.getstringextra ("name"); String pwd = Get = = null? "": Get.getstringextra ("pwd");((EditText) Findviewbyid (R.id.name_et)). SetText (name);(( EditText) Findviewbyid (R.id.pwd_et)). SetText (pwd);}}


2. Create a test project, right-click on the new-> in the Package Explorer area, select Android:android Test progec-Click the next-> Input Unit test project name Progect Progect name-> Click next-> to select the item under test (Testactivity.->finish in this example.

3. After the creation is complete, you can look at this empty project is what special, see Androidmanifest.xml, originally imported the test environment of Android

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "    package=" com.example.testactivity.test "    android:versioncode=" 1 "    android:versionname=" 1.0 " >    <uses-sdk android:minsdkversion= "8"/>    <instrumentation        android:name= " Android.test.InstrumentationTestRunner "        android:targetpackage=" com.example.testactivity "/>    < Application        android:icon= "@drawable/ic_launcher"        android:label= "@string/app_name" >        < Uses-library android:name= "Android.test.runner"/>    </application></manifest>
4. Create my test class, for Mainactivity's Mainactivitytest class, which inherits the ActivityInstrumentationTestCase2, which can be tested individually in an app's interface.

/** * mainactivity Test class * @author Jan */public class Mainactivitytest extendsactivityinstrumentationtestcase2< mainactivity> {private mainactivity mactivity;private instrumentation minstrumentation;private EditText nameEt; Private EditText pwdet;private button loginbutton;private button resetbutton;public mainactivitytest () {Super ( Mainactivity.class);} Initial setup @overrideprotected void Setup () throws Exception {Super.setup (); LOG.I ("Mainactivitytest", "---setUp---");//Avoid manual touch, turn off Touchmodesetactivityinitialtouchmode (false); Minstrumentation = Getinstrumentation ();//Gets the measured activitymactivity = getactivity ();//Gets the control of this active interface Nameet = (EditText) Mactivity.findviewbyid (com.example.testactivity.r.id.login_name_et);p Wdet = (EditText) Mactivity.findviewbyid ( Com.example.testactivity.r.id.pwd_et); Loginbutton = (Button) Mactivity.findviewbyid ( Com.example.testactivity.r.id.login); Resetbutton = (Button) Mactivity.findviewbyid ( Com.example.testactivity.r.id.reset);} Test: Ensure that each control is not empty public void testprecondition () {assertnotnull (mactivity); Assertnotnull (Nameet); Assertnotnull (Pwdet); Assertnotnull (Loginbutton); AssertNotNull ( Resetbutton);} public void input () {///This way simulates the clicked event on the UI Mactivity.runonuithread (new Runnable () {@Overridepublic void run () { Nameet.requestfocus (); Nameet.performclick ();}); /wait for the main thread of the application being tested to synchronize with the threads of the test case. Minstrumentation.waitforidlesync ();//The key value of the analog input test data SendKeys (keyevent.keycode_t, Keyevent.keycode_e, keyevent.keycode_s,keyevent.keycode_t, Keyevent.keycode_n, Keyevent.keycode_a, Keyevent.keycode_m, Keyevent.keycode_e) mactivity.runonuithread (new Runnable () {@Overridepublic void run () { Pwdet.requestfocus ();p Wdet.performclick ();}); SendKeys (Keyevent.keycode_1, keyevent.keycode_2, Keyevent.keycode_3,keyevent.keycode_4, keyevent.keycode_5);} public void Testinput () {input ();//test the value entered is consistent with the predicted content assertequals ("TestName", Nameet.gettext (). toString ()); Assertequals ("12345", Pwdet.gettext (). toString ());} /** * Test login function */public void Testlogin () {input (); Minstrumentation.runonmainsync (New Runnable () {@Overridepublic void run () {loginbutton.requestfocus (); Loginbutton.performclick ();}});} /** * Test reset button function */public void Testreset () {input (); Minstrumentation.runonmainsync (new Runnable () {@Overridepublic void Run () {resetbutton.requestfocus (); Resetbutton.performclick ();}}); Assertequals ("", Nameet.gettext (). ToString ()); Assertequals ("", Pwdet.gettext (). ToString ());} Used to reclaim and release resources @overrideprotected void TearDown () throws Exception {log.i ("mainactivitytest", "---tearDown---"); Super.teardown ();}}
Similarly, you can do this to resultactivity:

public class Resultactivitytest extendsactivityinstrumentationtestcase2< resultactivity> {private resultactivity resultactivity;private EditText nameet,pwdet;public resultactivitytest () { Super (Resultactivity.class);} @Overrideprotected void SetUp () throws Exception {Super.setup ();//create Intent, pass the user's login information Intent Intent = new          Intent ();          Intent.putextra ("name", "HelloWorld");          Intent.putextra ("pwd", "123456"); Start fxresultactivity resultactivity = Launchactivitywithintent ("com.example.testactivity") by intent that carries user login information, Resul        Tactivity.class, intent);        Get UI Component Nameet = (EditText) Resultactivity.findviewbyid (Com.example.testactivity.r.id.name_et); Pwdet = (EditText) Resultactivity.findviewbyid (Com.example.testactivity.r.id.pwd_et); }public void Testlogininfo () {assertequals ("HelloWorld", Nameet.gettext (). toString ()); Assertequals ("123456", Pwdet.gettext (). toString ());}} 
5. You can run him, select the activitytest you want to run, right-->android Junit test, and then look at his manual-free test, which is executed sequentially, and you may find that you are not going to execute the testlogin after execution. That is because he has jumped to another interface, so testing other methods can first block out the Testlogin.


Well, this is only a small trick, the actual use of not much, just understand, of course, interested can try



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Android primary]android Unit test activity individually tested

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.