Android--junit Unit Test

Source: Internet
Author: User

Android--junit Unit Test

Objective

This blog post explains how to use JUnit for unit testing in Android development. First, let's look at what a junit,junit test is a white-box test, which is primarily the programmer's own functional testing of the developed method. JUnit is a set of frameworks that are also used in Android.

Junit

Using JUnit testing in Android is roughly divided into the following steps:

    1. Increase support for junit in Androidmanifest.xml and develop a test project package.
    2. Add a <uses-library...> node to the <application.../> node in Androidmanifest.xml, and the Name property is Android.test.runner.
    3. After you write the method that you want to test, create a new class that inherits Androidtestcase, where you write the test case code.
    4. Left mouse button on the test case method, Run as→android JUnit test.

Here are a few steps to explain in detail, create a new Android project, and in Androidmanifest.xml, add a instrumentation:

Specify the name and Targetpackage of the instrumentation:

Add <uses-library android:name= "Android.test.runner" to the <application.../> node/>

After the completion of the Androidmanifest.xml code is as follows:

1<?XML version= "1.0" encoding= "Utf-8"?>2<ManifestXmlns:android= "Http://schemas.android.com/apk/res/android"3Package= "Com.example.junittestdemo"4Android:versioncode= "1"5Android:versionname= "1.0">67<Uses-sdk8Android:minsdkversion= "8"9Android:targetsdkversion= "17"/>1011<Instrumentation12Android:name= "Android.test.InstrumentationTestRunner"13Android:targetpackage= "Com.example.junittestdemo">14</Instrumentation>1516<Application17Android:allowbackup= "true"18Android:icon= "@drawable/ic_launcher"19Android:label= "@string/app_name"20Android:theme= "@style/apptheme">21st<Uses-libraryAndroid:name= "Android.test.runner"/>2223<Activity24Android:name= "Com.example.junittestdemo.MainActivity"25Android:label= "@string/app_name">26<Intent-filter>27<ActionAndroid:name= "Android.intent.action.MAIN"/>2829<category android: Name= "Android.intent.category.LAUNCHER" />30 </intent-filter >31 </activity>32 </ application>33 Span style= "color: #008080;" >34 </manifest>  

Write a simple percentage of progress calculation method:

1PackageCom.example.service;23PublicClass Progressservice { 4  Progressservice () { 5 } 7 public Integer Getcurrentprogerss (double Current,  Max" { 8 Integer i= ( int) ((Current/max) * 100 9 return I;10 }11}       

Write a test class that needs to inherit androidtestcase and test for the percent method:

1PackageCom.example.junit;23ImportAndroid.test.AndroidTestCase;4ImportAndroid.util.Log;567ImportCom.example.service.ProgressService;89PublicClass ProgressservicejunitExtendsAndroidtestcase {10PrivateFinal String tag= "main";1112PublicProgressservicejunit () {13 // TODO Auto-generated constructor Stub}15 16 public void Getcurrentprogersstest () {17 progressservice progressservice=new Progressservice ();  LOG.I (TAG, pro.tostring ());  }21}       

Left-click Getcurrentprogersstest () method, select Android JUnit Test and, if you need to debug, select the Android junit test under Debug as:

When the execution succeeds, the green is displayed, and if it is a different color, an error occurs:

You can see the test results in the Logcat log:

Android--junit Unit 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.