Android Development Series (iv): How to perform unit tests on applications and android unit tests

Source: Internet
Author: User

Android Development Series (iv): How to perform unit tests on applications and android unit tests

In the process of developing an application, the corresponding unit test is necessary.

Next, we will write a simple Android Application to complete basic unit testing.


First, we need to create an Android Project named junit.

Then, create a service class, that is, the class PersonService. java to be tested:

package cn.itcast.service;public class PersonService {public void save(String username){String sub = username.substring(6);}public int add(int a,int b){return a+b;}}
In this class, we will test two methods: save (String username) and add (int a, int B ).

Next, we need to configure the AndroidManifest. xml file:

<? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "cn. itcast. files "android: versionCode =" 1 "android: versionName =" 1.0 "> <uses-sdk android: minSdkVersion =" 14 "/> <application android: icon = "@ drawable/ic_launcher" android: label = "@ string/app_name"> <! -- Used to introduce the Test library, which must be placed in the application tag --> <uses-library android: name = "android. test. runner "/> <activity android: label =" @ string/app_name "android: name = ". fileActivity "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> </application> <! -- Indicates the name of the target package to be tested and instrementation, which are placed outside the application --> <instrumentation android: name = "android. test. instrumentationTestRunner "android: targetPackage =" cn. itcast. files "/> </manifest>
The test class can be correctly used only after configuration.


Now we can start testing.

Create a test class PersonServiceTest. java that inherits the AndroidTestCase class:

package cn.itcast.junit;import junit.framework.Assert;import cn.itcast.service.PersonService;import android.test.AndroidTestCase;public class PersonServiceTest extends AndroidTestCase {public void testSave() throws Exception{PersonService service = new PersonService();service.save("123123");}public void testAdd() throws Exception{PersonService service = new PersonService();int actual = service.add(1, 2);Assert.assertEquals(3, actual);}}
After the test class is set up, we need to test whether the two methods can run correctly:

Open the Outline View --> select the method to be tested --> Run as --> Android JUnit Test:

We can check whether the program runs normally on the JUnit interface.




What software does the company develop Android applications, including source code management, continuous integration software, unit testing, and bug tracking?

If the source code management is simple development, SVN is commonly used, and git is useful for its complexity.
Jenkins for continuous integration. The plug-in attached to jenkins can perform static checks and checkstyle.
Unit Tests are generally integrated with development platforms, such as JUNIT.

The Bug system uses Bugzilla.

How does one implement the four buttons below for Android Application Development?

Decompiled the layout of 5.0 main_tab.xml.
<? Xml version = "1.0" encoding = "UTF-8"?> <TabHost android: id = "@ id/tabhost" android: layout_width = "fill_parent" android: layout_height = "fill_parent" xmlns: android = "<FrameLayout android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <LinearLayout android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <FrameLayout android: id = "@ android: id/tabcontent" android: layout_width = "fill_parent" android: layout_height = "0.0dip" android: layout_weight = "1.0"/> <TabWidget android: id = "@ android: id/tabs" android: visibility = "gone" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_weight = "0.0"/> <LinearLayout android: gravity = "bottom" android: layout_gravity = "bottom" android: orientation = "horizontal" android: id = "@ id/main_tab_group" android: background = "@ drawable/mmfooter_bg" android: paddingTop = "2.0dip" android: layout_width = "fill_parent" android: layout_height = "wrap_content"> <FrameLayout android: background = "@ null" android: layout_width = "0.0dip" android: layout_height = &...... remaining full text>

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.