71st: Android's unit test class

Source: Internet
Author: User

We should strive to make a difference. In this way, we can say that we do not have wasting time, and it is possible to leave our footprints on the sands of time. --Take a break from the world of Roentgen


This lecture: Unit Test class

In the Android application development process for unit testing, the individual in the process of doing the project, the unit testing is necessary to ensure that we write the correctness of the program.


First, unit test steps:

Step one: Add the <application></application> inside the Androidmanifest.xml

<uses-library android:name= "Android.test.runner"/>
Step two: Add outside the <application></application>

<uses-permission android:name= "Android.permission.RUN_INSTRUMENTATION"/>
<instrumentation android:name= "Android.test.InstrumentationTestRunner"        android:targetpackage= " Com.example.text2 "/>
Note: The packages and specified in the android:targetpackage= "Com.example.text2"
Package= "Com.example.text2" > in the same package.

Step three: Establish a unit test class inheritance Androidtestcase


There are two ways to do unit testing in Android app development, and here are the two methods of unit testing:

Second, unit test method One: The application of the engineering Unit test



Here is the Junitservice class ( with an Add method that calculates the and of two integer data )

public class Junitservice {public int add (int a,int b) {return a+b;}}

The following is the Myjnuittest class in the test package (Verify that the program results are consistent with what we expect)

public class Myjnuittest extends androidtestcase{public void Testadd () {junitservice s=new junitservice (); int Sum=s.add ( 1, 2); Assert.assertequals (3, sum);}}

Registering in the Androidmanifest.xml file

    <application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@s Tring/app_name "Android:theme=" @style/apptheme "> <span style=" color: #ff0000; " > <uses-library android:name= "android.test.runner"/></span> <activity android:name= "D Emo. Testdemoactivity "android:label=" @string/app_name "> <intent-filter> <act Ion Android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUN CHER "/> </intent-filter> </activity> <activity android:name=". Junitservice "></activity> </application> <span style=" color: #ff0000; " ><uses-permission android:name= "Android.permission.RUN_INSTRUMENTATION"/> <instrumentation android:na Me= "Android.test.InstrumentationTestRunner" android:targetpackage= "Com.example.text2 "/></span></manifest> 

Open the Outline window in Eclipse, which shows all the methods of the Unit test class and then want to test which method, then right-click on the test method, select Run as, and then select Android JUnit Test, if it is normal, it will be as follows:


If there is an exception or error, the following occurs:



Three, unit test method two:

This approach is simpler, and this is done by creating a single unit test project for testing. That is, to create an Android test project, and then select the project that requires unit testing, so that the unit test in this way without the above configuration, the creation of such a project, the system has been configured to help us by default. In the last lecture we have explained in detail.


This is where we go, take your time and enjoy it



71st: Android's unit test class

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.