Android projects use Eclipse for unit testing

Source: Internet
Author: User

Android Projects If you debug the whole time, it will take a long time to load the UI. So the unit test is very convenient.

To perform unit tests, you first need to modify the next Androidmanifest.xml file. In the Instrument tab, click the Add button on the right. then double-click Instrument in the Pop-up window. Then on the right side there will be something more to depend on. Select the second line of the target package browse ..., select the packages to test. or the package to which the class to test belongs. I choose the thing here Com.example.scrollview package, and then select the first row Name after the browse, may search for a while, and then there will be a "android.test.InstrumentationTestRunner" Select it, and then save the androidmenifest.xml.

Then open the Androidmenifest.xml tab and find a line with the following code on the application label

<instrumentation android:name= "Android.test.InstrumentationTestRunner" android:targetpackage= " Com.example.scrollview "></instrumentation>

To do a unit test you have to add something, in the application, add the following line.

<uses-library android:name= "Android.test.runner"/> Where the value of Android:name can be alt+/to let Eclipse help you to automatically add it in.

Next you'll write the test code.

Build a test class under the package you just selected, or it can be in the package's sub-package. This test class inherits androidtestcase such as MyTest can write test methods in his class. When you are done, right-click on the method name and select Run as= Android JUnit tset. You can also debug, choose a breakpoint, right-click the method name, select Debug as = "Android JUnit tset

 Packagecom.example.scrollview.test;Importorg.apache.http.client.HttpClient;ImportOrg.apache.http.protocol.HTTP;ImportCom.example.scrollview.utils.HttpUtil;Importandroid.test.AndroidTestCase;Importandroid.text.Html;ImportAndroid.util.Log; Public classMyTestextendsandroidtestcase {String Testurl= "Http://192.168.1.2/post.php?key=name"; String params= "name=xiaoming&weight=78";  Public voidTesthttpget () {String message=Httputil.sendgetmethod (Testurl, params, HTTP.                UTF_8); LOG.D ("Shang", "Testhttpget:" +html.fromhtml (message)); }         Public voidTesthttppost () {String message=httputil.sendpost (Testurl, params, HTTP.        UTF_8); LOG.D ("Shang", "Testhttppost:" +html.fromhtml (message)); }}

This requires a mobile phone, or a simulator, but does not actually invoke the UI, so it is very fast, and the main reason is that he can test a class or method independently.

Android projects use Eclipse for unit testing

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.