Android app-Android unit test

Source: Internet
Author: User

Android app-Android unit test
In actual development, the process of developing android software must be continuously tested. Therefore, it is extremely important to master the unit test of Android. You should take unit tests as part of the Android application development cycle. well-written tests can help you find errors early in development. For more information about Android unit Testing, see Google's official Tutorial: Best Practices for Testing (FQ required) 1. Create Android Test Project 1 and create an Android Project: Hello. And change the layout file to the following: copy the Code 1 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "vertical"> 6 7 <TextView 8 android: id = "@ + id/text" 9 android: layout_width = "match_parent" 10 android: layout_height = "wrap_content" 11 android: text = "@ string/hello_w Orld "/> 12 13 <! -- It is not good to write strings directly in this way. It is recommended to write strings in strings. xml --> 14 <EditText15 android: id = "@ + id/edit" 16 android: layout_width = "match_parent" 17 android: layout_height = "wrap_content" 18 android: hint = "enter"/> 19 20 </LinearLayout> copy code 2. Create a test project for Hello: file-> New-> Other-> Android Test Project. The "New Android Test Project" dialog box is displayed. In "Project Name", enter "HelloTest ", click "Next ". 3. In Select Test Target, Select the Android project you want to Test and click Finish. After creating the Android Test Project, open the AndroidManifest. xml file of the HelloTest Project and you will find that there are more configurations than HelloTest. For details, see the following code: Copy code 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <manifest xmlns: android = "http://schemas.android.com/apk/res/android" 3 package = "com. lsj. hello. test "4 android: versionCode =" 1 "5 android: versionName =" 1.0 "> 6 7 <uses-sdk android: minSdkVersion =" 8 "/> 8 9 <! -- Indicates the Startup Device of the unit test framework. There are several types of start devices. You can choose from them. Generally, we use the above --> 10 <! -- TargetPackage is the same as the package of Hello, indicating that the unit test framework and the current application are in the same process --> 11 <instrumentation12 android: name = "android. test. instrumentationTestRunner "13 android: targetPackage =" com. lsj. hello "/> 14 15 <application16 android: icon =" @ drawable/ic_launcher "17 android: label =" @ string/app_name "> 18 <! -- Introduce some dependent libraries in the unit test framework --> 19 <uses-library android: name = "android. test. runner "/> 20 </application> 21 22 </manifest> copy code 2. Write unit Test code 1. After creating the Android Test Project, you will find that HelloTest automatically creates com. lsj. hello. the name of the tests package. The packages, classes, and methods for Android unit test are named as follows: package name: com. lsj. example ---- com. lsj. example. tests // corresponding package name + ". tests "Class Name: Hello. java ---- HelloTest. java // corresponding class name + "Test" method name: Hello () ---- testHello () // "test" + corresponding method name 2. lsj. hello. create MainActivityTest under the tests package. java class. MainActivityTest inherits from ActivityInstrumentationTestCase2. for Android testing classes, see the link at the end of the article. 3. Edit MainActivityTest and add the unit test code to copy code 1 package com. lsj. hello. test; 2 3 import android. app. instrumentation; 4 import android. test. activityInstrumentationTestCase2; 5 import android. view. keyEvent; 6 import android. widget. editText; 7 import android. widget. textView; 8 9 import com. lsj. hello. mainActivity; 10 11/** 12 * ActivityInstrumentationTestCase2 inherits from TestCase, And the TestCase class is located in junit. 13 * under the framework Package, you can see that all Android test classes are inherited from TestCase. To create a unit test, follow these steps: 14*15 * <pre> 16*1. inherit the TestCase class or its subclass 17*2. Define the instance variable 18*3. Use the setUp () method, initialize the variables and test environment before testing 19 *.... test 20*4. Garbage collection and other Aftercare Work after test completion 21 * </pre> 22*23 * @ author Lion 24 */25 public class MainActivityTest extends 26 ActivityInstrumentationTestCase2 <MainActivity> {27 28 private MainActivity mActivity; 29 private TextView mTextView; 30 private EditText mEditText; 31 private Inst Rumentation mInstrumentation; 32 33/** 34 * override the MainActivityTest constructor. Note that the wildcard class in super must be <>. Otherwise, an error is returned. 35 * according to the example officially provided by Google, its constructor is lunch, and during the test, it is found that a constructor with parameters will cause the unit test to fail. 36 */37 public MainActivityTest () {38 super (MainActivity. class); 39} 40 41 @ Override 42 protected void setUp () throws Exception {43/* initialize the variables and test environment */44 super. setUp (); 45 // close touch mode, otherwise the key event will be ignored 46 setActivityInitialTouchMode (false); 47 48 mInstrumentation = getInstrumentation (); 49 50 // obtain the tested MainActivity 51 mActivity = this. getActivity (); 52 // obtain the tested TextView control 53 mTextView = (TextView) MActivity. findViewById (com. lsj. hello. r. id. text); 54 // obtain the tested EditText control 55 mEditText = (EditText) mActivity. findViewById (com. lsj. hello. r. id. edit); 56} 57 58/** 59 * test whether data Initialization is null 60 */61 public void testInit () {62 // whether mActivity is null 63 assertNotNull (mActivity); 64 // whether mTextView is null 65 assertNotNull (mTextView ); 66 // assert whether mEditText is null 67 assertNotNull (mEditText); 68} 69 70/** 71 * test whether the text box string is equal 72 */73 public void testTextViewString () {74 // assert whether the text displayed by mTextView matches String. in xml, hello_world equals 75 assertEquals (76 mActivity. getResources (). getString (77 com. lsj. hello. r. string. hello_world), mTextView 78. getText (). toString (); 79} 80 81/** 82 * test input 83 */84 public void testEditTextInput () {85 input (); 86 assertEquals ("hello", mEditText. getText (). toString (); 87} 88 89/** 90 * simulated input 91 */92 Public void input () {93/* operations related to the UI component must be performed on the UI thread, so the runOnUiThread method of the Activity */94 mActivity is used. runOnUiThread (new Runnable () {95 @ Override 96 public void run () {97 mEditText. requestFocus (); 98 mEditText. optional mclick (); 99} 100}); 101/* 102 * because the test case is executed on a separate thread, the application needs to be synchronized here, 103 * The waitForIdleSync operation can be performed only when the test thread and UI thread are synchronized. 104 * waitForIdleSync and sendKeys cannot run 105 */106 mInstrumentation In the UI thread. waitForIdleSync (); 107 // call the sendKeys method and enter 108 sendKeys (KeyEvent. KEYCODE_H, KeyEvent. KEYCODE_E, KeyEvent. KEYCODE_L, 109 KeyEvent. KEYCODE_L, KeyEvent. KEYCODE_O); 110} 111 112 @ Override113 protected void tearDown () throws Exception {114/* garbage collection after testing */115 super. tearDown (); 116} 117}

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.