Android Program Test

Source: Internet
Author: User

First, the establishment of test environment

Eclipse, which installs the Android Developer Tools (ADT) plugin, will provide a graphical interface-based integrated development environment for you to create, build, and run Android programs. One convenient feature of Eclipse is the ability to automatically create a corresponding test project for your Android app project.

1. Build an Android project (named Androidnormalproject) or import an existing project.


2. Generate a test project corresponding to the project

On the project, right-click-->android tools-->new Test Project ...


Ii. Creating and executing test cases

Activity is written in a structured way. Be sure to put your test code in a separate package so that it is separate from the code it is testing.

By convention, the name of your test package should follow the same naming method as the app package name, followed by ". Tests" after the app package name. In the test package that you created, add the Java class for your test case. By convention, your test case name should also follow the same name as the Java or Android class you want to test, but with the suffix "test".

1. New test package and corresponding test Case name (Project package name. Test)

Project Package:


Test Package:


2. Set up Test fixture (Fixture)

Test fixture The object must be initialized by one or more running tests. To create a test fixture, you can rewrite the setup () and teardown () methods in your tests. The test automatically calls the Setup () method before any other test methods are run. You can use these methods to keep the test initialization and cleanup of the code separate.

Import Android.test.activityinstrumentationtestcase2;import Android.widget.textview;import Com.example.androidnormalproject.mainactivity;import Com.example.androidnormalproject.r;public Class Mainactivitytest extends Activityinstrumentationtestcase2<mainactivity>{private MainActivity mMainActivity; Private TextView mtextview;public Mainactivitytest () {super (mainactivity.class);} @Overrideprotected void SetUp () throws Exception {Super.setup (); mmainactivity = Getactivity (); if (mmainactivity = = null) Return;mtextview = (TextView) Mmainactivity.findviewbyid (r.id.helloword_text);} @Overrideprotected void TearDown () throws Exception {Super.teardown ();}}

3. Add a test premise

Before the formal testing, we will ensure that the tested objects are instantiated and initialized correctly, adding a testpreconditions () method

public void Testpreconditions () {    assertnotnull ("mmainactivity is null", mmainactivity);    Assertnotnull ("Mtextview is null", Mtextview);}
4. Add a test method to verify activity

public void Testmainactivitytextview_helloword () {final String expected = mmainactivity.getstring (r.string.hello_ World); final String actual = Mtextview.gettext (). toString (); Assertequals (expected, actual);}

5. Run the test

Run AS----Android Junit Test

Third, testing the UI component

1. Test the layout of a button

/** * Test the layout of the button */public void Testmainactivitybutton_layout () {final View Decorview = Mmainactivity.getwindow (). Getdecorview ();//Whether in-screen    viewasserts.assertonscreen (Decorview, Mbutton);    Final Viewgroup.layoutparams layoutparams =            mbutton.getlayoutparams ();    Assertnotnull (layoutparams);    Test layout Properties    assertequals (Layoutparams.width, WindowManager.LayoutParams.MATCH_PARENT);    Assertequals (Layoutparams.height, WindowManager.LayoutParams.WRAP_CONTENT);}
2. Verify the behavior of the button

/** * Test button click event */public void Testmainactivitybutton_onclick () {String Expectedinfotext = mmainactivity.getstring ( R.string.button_text);    Touchutils.clickview (this, Mbutton);    Asserttrue (view.visible = = Mbutton.getvisibility ());    Assertequals (Expectedinfotext, Mbutton.gettext ());}
Note: The above touchuitl is a safe way to send events from other threads to the UI thread and should not be used in the UI thread.

3. Application Test Notes

@SmallTest

A part of a small test that marks a test run

@MediumTest

Marks a part of a test run's medium test

@LargeTest

A part of a large test that marks a test run

Reprint please indicate source: Http://blog.csdn.net/dawanganban

Summary: Using the test method described above, we can build a unit test that is suitable for us (the least likely code block in the test code) and a functional test (to verify that a single application component works with other components as the user expects)

Android Program Test

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.