[Android development-6] For more information, we need some debugging and testing methods, android-6

Source: Internet
Author: User

[Android development-6] For more information, we need some debugging and testing methods, android-6

Preface:Life cannot be perfect. There will always be some regrets. We can understand what life is after we have had regrets. The same is true for programs. In pursuit of perfection, there will inevitably be bugs. Through continuous bug training, our technology will continue to grow. When debugging a bug, you can use some methods and methods to find that it is the same. When everything is suddenly realized, we will find that defects are also a kind of beauty, because they allow you to better understand yourself or your programs.



1. Print Output debugging

When the Android program runs on a virtual machine, if we output debugging information through System. out. print (), we cannot see it on the console. So sometimes we need to output something in the background for debugging. What should we do. It doesn't matter. Android provides a powerful android. util. Log class. We can use the following:


Log. v ("verbose", "details level-Level 1 ");
Log. d ("debug", "debugging information level-Level 2 ");
Log. I ("info", "prompt information level-Level 3 ");
Log. w ("warn", "Warning Level-Level 4 ");
Log. e ("error", "error information level-Level 5 ");


The first parameter in Log: indicates a Tag description; the second parameter: Indicates debugging information.


After the program runs, LogCat In the Eclipse interface will output the following content:



From the above we can see that different levels and colors represent different, which makes debugging easy to observe. If the interface contains many debugging logs of the same type, you can add a filter to the green + TAB on the left to display only the debugging logs of a Tag, as shown in the following, only the Tag marked as debug is displayed.




2. Unit Test

Unit test, demonstration steps:

1. Create a project, such as TestAndroidJunit. The directory is as follows:


2. A project generally has multiple business function classes. For business function classes, sometimes a team needs to verify the correctness before submitting the project to svn. At this time, the unit test debugging bug is very important. Right-click src and create a class MyFunction as follows:

package com.wyz.myfunction;import android.util.Log;public class MyFunction {public void SayHello(String str){Log.d("SayHello",str);}public int Add(int a,int b){return a+b;}}

3. Add the following content to the AndroidManifest. xml file in TestAndroidJunit:

<Uses-library android: name = "android. test. runner"/>

<Instrumentation
Android: name = "android. test. InstrumentationTestRunner"
Android: targetPackage = "com. wyz. testandroidjunit"/>


Remember to add the following locations:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.wyz.testandroidjunit"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="17"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >               <activity            android:name=".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><instrumentation          android:name="android.test.InstrumentationTestRunner"          android:targetPackage="com.wyz.testandroidjunit" />  </manifest>
Note: The android: targetPackage Attribute Value Setting in <instrumentation> must be consistent with the package name of the project, that is

<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. wyz. testandroidjunit"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">

Package = "com. wyz. testandroidjunit"


4. Right-click the src folder in the project to create a test unit class and set it as follows:


Click Browser to browse the inheritance class AndroidTestCase



5. Write the content in the unit test class as follows, mainly to test the method of MyFunction in the project:

package com.wyz.testunit;import junit.framework.Assert;import com.wyz.myfunction.MyFunction;import android.test.AndroidTestCase;import android.util.Log;public class TestMyFunction extends AndroidTestCase {public void testSayHello() throws Exception{MyFunction myFunction = new MyFunction();myFunction.SayHello(null);}public void testAdd() throws Exception{MyFunction myFunction = new MyFunction();int c = myFunction.Add(3, 4);Log.d("add", String.valueOf(c));Assert.assertEquals(7, c);}}


6. After writing the above Code in the unit test class, we can test the two methods in the MyFunction class and find the Outline View:



If we want to Test testAdd (), we only need to find the corresponding testAdd () in Outline, and then right-click, Run As-> Android Junit Test,

If the result is correct, it is displayed as follows:


To display an error, you only need to change 7 in the assertion to another number to demonstrate the error.


Note: you must first run the android simulator and then the unit test module so that the unit test module can be deployed and installed.


There are many debugging and testing methods, as well as writing files and debugging on the real machine. Many things have to be practiced step by step to be recognized!



How to debug a real machine during android Development

Enable USB debugging

After connecting to the computer, eclipse runs the program as the interface. If multiple simulators (real machine + simulator) are enabled, a selection will pop up to which simulator the program is installed.

Where are the Android development materials better? Debugging skills

We recommend that you go to the android development case-driven tutorial, which is detailed.
There is also a forum eoeandroid forum, which Baidu searches for, which contains the latest android Information and Learning works and teaching for android programming. If you have any questions, you can also ask questions. It is a good learning platform.
Hope to help you.

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.