zoukankan      html  css  js  c++  java
  • 【Android开发-6】了解内情,我们须要一些调试和測试手段

    前言:人生不可能十全十美,总会有些遗憾存在,经历过遗憾,我们才懂的什么是生活。

    程序也一样。追求完美,就必定会有经历bug存在的时候。

    经历过不断的bug磨练。我们技术才会不断的成长。对于调试bug,通过一些方法和手段就会发现它原来如此。

    当一切恍然大悟时。就会发现缺陷也是一种美,由于它让你更了解自己。或者说让你更加了解你的程序。



    第一、打印输出调试

    Android程序在虚拟机执行时,我们假设通过System.out.print(),输出调试信息。我们在控制台是看不到的。所以我们有时候调试,后台要输出一些东西,该怎么办。没关系,Android提供了一个强大的android.util.Log 类. 我们能够运用例如以下:


    Log.v("verbose","具体信息级别--1级");
    Log.d("debug","调试信息级别--2级");
    Log.i("info","提示信息级别--3级");
    Log.w("warn","警告信息级别--4级");
    Log.e("error","错误信息级别--5级");


    Log中的第一个參数:代表一个标签Tag说明 。第二个參数:代表调试信息


    程序执行后,会在Eclipse界面中的LogCat输出例如以下内容:



    从上面能够看出:不同级别,颜色代表也不同,这样就易于调试观察了。假设界面有非常多类型同样的调试日志信息,左边栏点绿色的+。还能够加入过滤器,仅仅显示某标签Tag的调试日志,如以下仅仅显示Tag标签为debug的




    第二、单元測试

    单元測试,演示步骤:

    1.新建一个项目,比方叫TestAndroidJunit,建完文件夹例如以下:


    2.一个项目中一般有多个业务功能类,对于业务功能类,有时候一个团队。个人弄完须要验证正确后。才提交到svn中。

    这时候单元測试调bug就非常重要了。

    右键src,新建class类MyFunction例如以下:

    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.在TestAndroidJunit中AndroidManifest.xml的文件增加例如以下内容:

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

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


    记住加的位置例如以下:

    <?

    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>

    注:上面的<instrumentation>中的android:targetPackage属性值设置需跟本项目的包名一致,即

    <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"


    4.右键项目中src目录新建一个測试单元类,设置例如以下:


    点Browser浏览继承类AndroidTestCase



    5.单元測试类中的内容编写例如以下,主要是对项目中MyFunction的方法測试

    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.在单元測试类写好上述代码后,我们就能够对MyFunction类中的两个方法进行測试,找到Outline视图:



    假设我们要測试testAdd(),我们仅仅需在Outline中找到相应的testAdd()。然后右击。Run As->Android Junit Test,

    结果假设正确。显演示样例如以下:


    假设要显示错误,仅仅需把断言中的7改成其它数字就能够演示出错误了。



    注:上面必须先执行安卓模拟器,然后执行单元測试模块。这样单元測试模块才干够部署安装。


    调试和測试方法非常多,还实用写入文件。真机调试等。很多东西还得一步步实践,才干认知!


  • 相关阅读:
    leetcode 347. Top K Frequent Elements
    581. Shortest Unsorted Continuous Subarray
    leetcode 3. Longest Substring Without Repeating Characters
    leetcode 217. Contains Duplicate、219. Contains Duplicate II、220. Contains Duplicate、287. Find the Duplicate Number 、442. Find All Duplicates in an Array 、448. Find All Numbers Disappeared in an Array
    leetcode 461. Hamming Distance
    leetcode 19. Remove Nth Node From End of List
    leetcode 100. Same Tree、101. Symmetric Tree
    leetcode 171. Excel Sheet Column Number
    leetcode 242. Valid Anagram
    leetcode 326. Power of Three
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6800219.html
Copyright © 2011-2022 走看看