Android -- junit测试框架,logcat获取log信息

1. 相关概念

白盒测试:
知道程序源代码.

 根据测试的粒度分为不同的类型
  方法测试 function test
        单元测试 unit test        
        集成测试 intergation test
 根据测试的次数分为不同的类型
        冒烟测试 smoke test (adb shell monkey 测试点击N次)
        压力测试 pressure test

黑盒测试:
8k
不知道程序的源代码,主要操作应用程序的业务逻辑.

logcat
System.out.println();

日志信息等级
verbose  提示
debug    调试
info   
warn     警告
error    错误

2. 示例代码

CalcService.java, 需要测试的service类

public class CalcService {
	private static final String tag = "CalcService";
	/**
	 * 相加的业务方法
	 * @param x 第一个int
	 * @param y 第二个int
	 * @return
	 */
	public int add(int x ,int y){
		Log.v(tag, "x="+x);
		Log.d(tag, "y="+y);
		int result = x+y;
		Log.i(tag,"result="+result);
		Log.w(tag,"result="+result);
		Log.e(tag,"result="+result);
		
		System.out.println("out result="+result);
		System.err.println("error result="+result);
		
		return result;
	}
}

TestCalcService.java 测试类,继承androidTestCase

public class TestCalcService extends AndroidTestCase {

	/**
	 * 所有的测试方法  应该把异常抛给测试框架
	 * @throws Exception
	 */
	public void testAdd() throws Exception{
		CalcService calcService = new CalcService();
		int result = calcService.add(3, 5);
		assertEquals(8, result);
	}
}

AndroidManifest.xml,要使用junit需要配置uses-library 和 instrumentation

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima.junit"
    android:versionCode="1"
    android:versionName="1.0" >

<instrumentation
    android:name="android.test.InstrumentationTestRunner"
    android:label="Tests for My App"
    android:targetPackage="com.itheima.junit" />
    
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <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="com.itheima.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>



 

原文地址:https://www.cnblogs.com/xj626852095/p/3647987.html