Two ways to unit test in Android development

Source: Internet
Author: User

Androidtwo ways of unit testing in development

A good programmer is also unable to ensure that their programs do not have bugs, so it is absolutely necessary to write the appropriate test procedures, which will reduce the program in the late emergence of a variety of weird bugs , reduce maintenance costs, Take precautions to nip Bugs in the cradle.

See a lot of online still write Java Unit test in the way of writing android Program Unit test program-junit, Of course, I've always hated the idea of moving outdated things to new technology applications to get a place, and constantly applying new methods to improve efficiency, and perfecting procedures is the truth! Not much to say, directly to the focus of today: Two methods of unit testing in Android development; The first is to attach directly to the project, and the second is to create a new android test Project. Both of these methods are good and similar, of course, the latter is more formal, but also we should grasp.


The first unit test method:

Put the test class directly in the project being tested

Write a test class that is placed directly in the engineering junitest that needs to be tested , and of course it is necessary to implement the relevant configuration for the purpose of testing the project, before I make a brief description of the project's code:junitest Project Source Mainactivity.java is self-contained without modification,Personservice.java is a simple function class for testing purposes:

Package Com.example.server;public class Personservice {public Personservice () {//TODO auto-generated constructor stub} public void Save (string username) {String sub = username.substring (6);} public int Add (int a, int b) {return a+b;}}

The point is to test whether the two functions in Personservice.java save and add function properly.


Well, after understanding the engineering code we began to configure the preparation of the test unit, altogether two points:

1, inAndroidmanifest.xmlin the file:

<uses-library android:name= "Android.test.runner"/>
This line is placed inApplicationAmong the nodes

<instrumentation        android:name= "Android.test.InstrumentationTestRunner"        android:targetpackage= " Com.example.server "        android:label=" This is my test class "></instrumentation>
This node is placed in aApplicationnode, note that theCom.example.serverReplace the package name with the project being tested.

2. Write Test class

I am a test.java do the demo, this class of writing needs to be aware of must inherit a test framework class a Ndroidtestcase; then write the test function for each function of the test project in this inheritance class.

Here I post the androidmanifest.xml and test classes I have configured:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Com.example.server "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/> <application android:allowbackup=" t Rue "android:icon=" @drawable/ic_launcher "android:label=" @string/app_name "android:theme=" @style/appt Heme "> <activity android:name=" com.example.server.MainActivity "android:label=" @string                  /app_name "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/>        <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <uses-library android:name= "Android.test.runner"/> </application> &l T;instrumentation android:name= "Android.test.InstrumentationTestRunner "android:targetpackage=" Com.example.server "android:label=" This is my Test Class "></instrumentation></manifest>
and test Cases:

Package Com.example.test;import Junit.framework.assert;import Com.example.server.personservice;import Android.test.androidtestcase;public class Test extends Androidtestcase {public void Testsave () throws exception{ Personservice service = new Personservice (); Service.save (null);} public void Testadd () throws Exception{personservice service = new Personservice (); int actual = Service.add (1, 2); Assert.assertequals (3, Actual);}}

Then we click on the test class, in the correspondingOutlineThe box should be able to see the corresponding function

Select the function you want to test, select Run as----and Android Junit test then select the specified emulator or the real machine should be able to use the Junit bar corresponding to the indicator to analyze the function tested to meet the requirements.

(green indication is correct)


(red indicates an error, followed by printing the details of the error)

Add: Just when doing the demo suddenly always appeared test fail, no test resault. Always need to find the problem, and finally the magic of the normal ... Write a program not afraid of mistakes, afraid of the wrong, normal confused! The only problem is that I deleted the constructor by myself ... It seems that the program should be carefully cared for.


The second unit test method

Write an android test project alone to test

This is actually a more normative approach, and it needs to be mastered, and relatively simple and provincial.

1, the first is the newAndroidTest Engineering

2, follow the prompts to choose which project you want to test:

3and finally create a new test class directly

test it as you said in method one. When we opened the Androidmanifest.xml file for this test class , we found that we had already configured our own configuration, which is the convenient way to test this.

Finally: On the unit test, I summarize several points:

1, configuration Androidmanifest.xml, of which there are only two

2. Inheriting androidtestcase

3, write the test function must be void public , and to throws Exception because in the test framework, this writing facilitates the test framework to get exception information

4, write the test function name is best for testing + The name of the letter to be tested

The source code in this article is downloaded

Two ways to unit test in Android development

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.