"Android Development-6" in the insider, we need some debugging and testing tools

Source: Internet
Author: User

Preface: life can not be perfect, there will always be some regrets exist, experienced regret, we understand what is life. The same procedure, the pursuit of perfection, will inevitably experience the existence of a bug. Experienced the constant bug hone, our technology will continue to grow. For debugging bugs, there are ways and means to find it. When everything comes to a sudden, the flaw is also a beauty, because it lets you know more about yourself, or let you know more about your program.



First, print output debugging

When the Android program is running on the virtual machine, we cannot see it in the console if we output debug information via System.out.print (). So we sometimes debug, the background to output something, what to do. It doesn't matter, Android offers a powerful Android.util.Log class. We can use the following:


LOG.V ("Verbose", "level of detail-level 1");
LOG.D ("Debug", "Debug information Level--level 2");
LOG.I ("info", "Cue information level--level 3");
LOG.W ("Warn", "warning message level--level 4");
LOG.E ("Error", "Fault message level--level 5");


The first parameter in log: Represents a tag tag description; The second parameter: represents the Debug information


After the program runs, the Logcat in the Eclipse interface will output the following:



As can be seen from the above: different levels, color representatives are also different, so it is easy to debug observation. If the interface has many types of the same debug log information, the left column point green +, you can also add a filter, only display a tag tag debug log, as shown below only the tag tag for debug




Second, Unit testing

Unit test, demonstration steps:

1. Create a new project, for example, called Testandroidjunit, and complete the following directories:


2. There are typically multiple business function classes in a project, and for a business function class, sometimes a team is required to verify the correct one before committing to SVN. It is important that the unit test tune the bug. Right-click SRC, the new class 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. InTestandroidjunit The Androidmanifest.xml file, add the following:

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

<instrumentation
Android:name= "Android.test.InstrumentationTestRunner"
Android:targetpackage= "Com.wyz.testandroidjunit"/>


Remember to add the following location:

<?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 "> &LT;USES-SD K android:minsdkversion= "android:targetsdkversion="/> <application android:allowback Up= "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.run Ner "/> </application><instrumentation android:name=" android.test.iNstrumentationtestrunner "android:targetpackage=" Com.wyz.testandroidjunit "/> </manifest> 
Note: The Android:targetpackage attribute value setting in <instrumentation> above must be the same as the package name of this project, i.e.

<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.wyz.testandroidjunit"
Android:versioncode= "1"
Android:versionname= "1.0" >

package= "Com.wyz.testandroidjunit" in the


4. Right-click the SRC folder in the project to create a new test unit class, set as follows:


Click Browser to browse inheriting classes Androidtestcase



5. The contents of the Unit test class are written as follows, mainly the method Test of the class 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 the Unit test class has written the above code, we cantest the two methods in the MyFunction class to find the outline view:



If we want to test testadd (), we just need to find the corresponding testadd () in outline, then right-click, Run as->android Junit test,

If the result is correct, it appears as follows:


If you want to display an error, simply change the 7 in the assertion to another number to demonstrate the error.


Note: You must first run the Android emulator and then run the Unit test module so that the unit test module can deploy the installation.


Many debugging and testing methods, but also useful to write files, real-machine debugging, and many things have to step-by-step practice, in order to recognize!


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.