[android] 01_Android中单元测试

Android 4.0

Android中单元测试


一、测试介绍

好的软件不是写出来, 测出来的.
 
1、根据测试的时候 是否有源代码:
 
白盒测试: 写测试代码 测试源代码里面的业务方法
黑盒测试: 不知道源代码, 测试应用程序的业务逻辑.
 
2、根据测试的粒度
 
方法测试  function test
 
单元测试  unit test   junit测试框架
 
集成测试  integration test
 
系统测试  system test
 
 
3、根据测试的暴力程度
 
冒烟测试 smoke test
压力测试 pressure test
 
android中的压力测试
-->adb shell
-->monkey 5000

二、Android中单元测试

google提供了一个android下的测试框架,
    1、首先编写测试代码-->2、测试代码给上传到手机-->3、在dalvik虚拟机里面执行测试代码
 
android下的测试代码步骤
    1.写一个测试类 继承androidtestcase
    2.在manifest清单文件配置 instrumentation     uses-library
 
在实际开发中,开发android软件的过程需要不断地进行测试。而使用Junit测试框架,侧是正规Android开发的必用技术,在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性。
第一步:首先在AndroidManifest.xml中加入下面红色代码:
 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cn.itcast.action“ android:versionCode="1“  android:versionName="1.0">
 <application android:icon="@drawable/icon" android:label="@string/app_name">
        <uses-library android:name="android.test.runner" />
        ....
 </application>
 <uses-sdk android:minSdkVersion="6" />
 <instrumentation
    android:name="android.test.InstrumentationTestRunner"
      android:targetPackage="cn.itcast.action" android:label="Tests for My App" />
</manifest>
上面targetPackage指定的包要和应用的package相同
第二步:编写单元测试代码选择要测试的方法,右键点击Run As”--“Android Junit Test” ):
import cn.zengfansheng.service.CalcService;
import android.test.AndroidTestCase;
public class TestCalcService extends AndroidTestCase {
    public void testAdd(){
        CalcService calc = new CalcService();
        int temp = calc.add(3, 5);
        assertEquals(8, temp);
    }
}

三、问题:没有在AndroidManifest.xml清单文件中配置

单元测试 does not specify a android.test.InstrumentationTestRunner instrumentation 

or does not declare uses-library android.test.runner in its AndroidManifest.xml

解决:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.zengfansheng.junit"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <instrumentation 
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="cn.zengfansheng.junit"
        ></instrumentation>
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="android.test.runner"/>
        <activity
            android:name="cn.zengfansheng.junit.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>
    </application>
</manifest>

四、如果忘记了上面两句怎么写,可以自己手动创建一个Test工程,工具会自动帮我们生产测试代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.zengfansheng.junit.test"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk android:minSdkVersion="8" />
 
    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="cn.zengfansheng.junit" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
 
        <uses-library android:name="android.test.runner" />
 
    </application>
 
</manifest>