Junit test in Android

Source: Internet
Author: User

In the development of Junit testing, it is very easy for developers to identify and handle problems as early as possible, and it is also very simple to use. Just import the jar packages related to Junit testing and create test classes, you can test the business functions without adding output statements to the Code for testing. Note that the written test method can only be public void and has no parameters. You can do whatever you want in the test method.

@Testpublic void testAdd(){    Demo d = new Demo();    System.out.println(d.add(1,2,3));}

Testing in the Android environment seems inconvenient. To write a test class, you must configure the test in the application project configuration. Fortunately, google provides an adt integration environment, which is no longer a problem.
Next, let's take a look at the configuration steps for Junit testing in an Android project:
1. In the AndroidManifest. xml file of the project, locate the <application> </application> section and add the following:

  <uses-library android:name="android.test.runner" />

 

2. In the AndroidManifest. xml file of the project, locate the <manifest> </manifest> section and add the following content:

  <intent-filter>      <action android:name="android.intent.action.MAIN" />      <category android:name="android.intent.category.LAUNCHER" />  </intent-filter>

3. Create a test class, inherit from AndroidTestCase, and write the test method in it. For example:

    public void testGetFirstName() throws Exception {        PersonService ps = new PersonService();        ps.getFirstName();    }    public void testCacl() throws Exception {        PersonService ps = new PersonService();        Assert.assertEquals(2, ps.calc());    }

4. You can finally choose runas> Android JUnit Test. The Junit window will display red bars or green bars. Some information is displayed in the Console, such:

[2014-05-26 17:04:47 - SlideDemo] ------------------------------[2014-05-26 17:04:47 - SlideDemo] Android Launch![2014-05-26 17:04:47 - SlideDemo] adb is running normally.[2014-05-26 17:04:47 - SlideDemo] Performing android.test.InstrumentationTestRunner JUnit launch[2014-05-26 17:04:47 - SlideDemo] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'XT531'[2014-05-26 17:04:47 - SlideDemo] Uploading SlideDemo.apk onto device 'emulator-5554'[2014-05-26 17:04:48 - SlideDemo] Installing SlideDemo.apk...[2014-05-26 17:04:49 - SlideDemo] Success![2014-05-26 17:04:49 - SlideDemo] Launching instrumentation android.test.InstrumentationTestRunner on emulator-5554[2014-05-26 17:04:50 - SlideDemo] Sending test information to Eclipse[2014-05-26 17:04:50 - SlideDemo] Test run finished

 

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.