Unit Test for Android

Source: Internet
Author: User

According to the Android developer documentation, the Android UT is a learning summary:

1. Build the eclipse test environment.

The Eclipse's ADT (Android Developer Tools) plugin gives us an integrated development environment for creating and running Android test cases through the GUI.

In Eclipse, create a test project for your Android app by following these steps:

A. Right-click Android app, select Android tools-> New Test Project

B. Set the appropriate properties for your test project, based on the Test Project Creation wizard.

2. Create Test Cases

It is a good idea to create a test class for each activity of the project.

Each test case consists of roughly three parts:

A. Test Fixture is a number of objects that need to be initialized to run your test method.

B. Preconditions test method, some pre-condition testing methods, to ensure that the object you are testing is properly initialized. There is also the setup (), TearDown () method, which calls the Setup () method before calling any other test methods, and calls the TearDown () method after invoking the other test methods. You can initialize the object you want to test in the Setup () method.

C. Test methods related to the specific activity.

Steps:

A. By default, when a test project is completed, you will create a call<your_app_package_name>.tests的包,如果没有,自己建一个。

B. In the package created in a, create a test class named for the activity you are going to test <your_app_activity_name>Test , 如MyFirstTestActivityTest .

C. write the Test class. The test class must inherit ActivityTestCase a subclass of the class, such as

 Publicclassmyfirsttestactivitytest
extendsActivityInstrumentationTestCase2<myfirsttestactivity>{

Privatemyfirsttestactivitymfirsttestactivity;//Activity you want to test
PrivateTextViewMfirsttesttext;

Publicmyfirsttestactivitytest(){
Super(myfirsttestactivity.class);
}

@Override
protectedvoidsetUp()throwsException{
Super. setUp ();//Must call the Setup method of the parent class, the requirements of JUnit
mfirsttestactivity
=getactivity();//Get a reference to the activity you want to test
Mfirsttesttext
=
(TextView)mfirsttestactivity
.Findviewbyid(R.ID.My_first_test_text_view);
}

        Publicvoidtestpreconditions(){//is used to ensure that your test Fixture, that is, whether the object you are testing is properly initialized.
Assertnotnull
("mfirsttestactivity isNULL",mfirsttestactivity); If False, throws an AssertionFailedError exception, the first parameter is used to output a hint, true, the test passes
Assertnotnull
("Mfirsttesttext isNULL",Mfirsttesttext); Whether true or FALSE, the test program calls other test methods
}

        PublicvoidTestmyfirsttesttextview_labeltext(){//Specific test class, placed after Testpreconditions () method--Initial validation is done first
FinalStringexpected=
Mfirsttestactivity
.getString(R.string.my_first_test);
FinalStringactual=Mfirsttesttext.GetText().toString();
Assertequals
(expected,actual);
}

}

D. Run the test case. Connect an emulator or phone to make sure the USB debugging is turned on in the Settings-developer options. Right-click your test class, select Run as--and Android Junit test. Then select the device you want to run and start running the test. Try to see the results in JUnit.

(not to be continued)

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.