Android Dev Unit Test (i)

Source: Internet
Author: User

Android Dev Unit Test (i)

Please respect other people's labor results, reproduced please indicate the source : unit Test of Android Development (i)

http://blog.csdn.net/fengyuzhengfan/article/details/40209995

In real-world development, the process of developing Android software needs to be constantly tested. Android unit Testing is a must-have step for regular Android development. Unit tests can be embedded in a project, or they can be tested for a specific project as a separate project.


1. Hierarchy of Android Unit test framework

It can be seen from:

The test methods in 1.android are mainly androidtestcase and instrumentationtextcase. So what is instrumentation?

Instrumentation and activity are a bit similar, except that the activity needs an interface, and instrumentation is not, we can understand it as a non-graphical interface, with the ability to boot, to monitor other classes ( The tool class declared with the target package.

2. The Android test class enables testing of important components in Android (such as Activity,service,contentprovider and application).


2. Steps for Android Unit testing


Here I write a simple test class Mathutil There is only one test method in this class sum used to calculate the sum of two integers.   

Package com.jph.simpleapp;/** * Tested class * @author JPH * date:2014.10.18 */public class Mathutil {public Mathutil () {//TODO A uto-generated constructor stub}public int sum (int a,int b) {return a+b;}}

Tips: the method being measured must be declared as Public , otherwise it cannot be accessed.

The following are the main steps to start creating a test project:

First step: Create a test project


To create an Android test project, in the Popup box, select Android test

Select the project you want to test, and then click Finish Finish can be.

at this point, Android The test project has been created, so let's take a look at the project manifest file,

as can be seen, Android Test engineering than normal Android more projects. <instrumentation/> Labels and < uses-library android:name="Android.test.runner"/>.

which Targetpackage represents the tested Android the package name of the project.

Step Two: Create a test class


Here you choose to inherit androidtestcase.


Test code:

Package Com.jph.simpleapp.test;import com.jph.simpleapp.mathutil;import android.test.androidtestcase;/** * Test class * @ Author JPH * date:2014.10.18 */public class Mathutiltest extends Androidtestcase {mathutil mutil;/** * The first method to be executed, which can be entered in this method Line some initialization operation * @see Android.test.androidtestcase#setup () */@Overrideprotected void SetUp () throws Exception {//TODO Auto-gen Erated method Stubmutil=new Mathutil (); Super.setup ();} public void Testsum () {int result=mutil.sum (2, 1); Assertequals (3, result); SYSTEM.OUT.PRINTLN ("Result:" +result);} /** * Last executed method, here are some finishing touches * @see Android.test.androidtestcase#teardown () */@Overrideprotected void TearDown () throws Exception {//TODO auto-generated method Stubsuper.teardown ();}}
 

Code Analysis:

SetUp (): The first method to be executed, which can be initialized in this method

TearDown (): The last method to be executed, where some finishing touches are done

Testsum (): is the method to test sum .

at this point, the test class has been written to completion. Start the Android unit test below.


Step Three: Conduct unit tests


We can either run a test method or all of the methods according to the required option, step

Operation Result:


Results Analysis:

as you can see, the result of the run is a failure because sum returned by 3 , but we expect it to return the 4 .


below we will change our expectations to 3 :



This is expected and the result is the same, run through. We pass the past 1 and 2 to add, the return result is 3, so the sum method is proved correct.

not to be continued ... .....


If you think this blog post is helpful to you, please give this blog a praise! You can also pay attention to Fengyuzhengfan's blog, watch more wonderful http://blog.csdn.net/fengyuzhengfan/


Android Dev Unit Test (i)

Related Article

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.