单元测试1(Androidf)


步骤:

      1.确定要测试的类(MathUtils)方法(incrment)。  --incrment

   不能直接在MathUtils类中的main方法直接测试,会有异常,如下:

If you would like to submit a bug report, please visit:
    http://java.sun.com/webapps/bugreport/crash.jsp#

       2.测试incrment方法,Test类需要继承AndroidTestCase

此时不能测试,测试会有如下错误:

JunitTest does not specify a android.test.InstrumentationTestRunner instrumentation or 
 does not declare uses-library android.test.runner in its AndroidManifest.xml 

       3.在清单文件中指定测试信息和要测试的包, 指定引用的测试包 

     

         

1.

<span style="font-size:14px;">package com.ithema28.junittest.test;

import com.ithema28.junittest.utlis.MathUtils;

import android.test.AndroidTestCase;

public class Test extends AndroidTestCase{
	
	 
	public void test(){
		//	System.out.println("test dsa ");
		
		int result = MathUtils.incrment(9, 10);
		//断言  ,断定某一个对象就是某一个值
		assertEquals(19, result);
	}
	/*
	  JunitTest does not specify a android.test.InstrumentationTestRunner instrumentation or 
	  does not declare uses-library android.test.runner in its AndroidManifest.xml 
	*/
	
}
</span>
2.


<span style="font-size:14px;">package com.ithema28.junittest.utlis;

import android.util.Log;

public class MathUtils {
	
	
	/**
	 * 加法运算
	 * */
	public static int incrment(int x,int y){
		/*
		 	这几个BUG依次增强
		 */
		Log.v("MathUtils", "黑色:" + x + " + " +   y   + " = " + (x+y));
		Log.d("MathUtils","蓝色:" + x + " + " +   y   + " = " + (x+y));
		Log.i("MathUtils", "绿色:" +x + " + " +   y   + " = " + (x+y));
		Log.w("MathUtils","黄色:" + x + " + " +   y   + " = " + (x+y));
		Log.e("MathUtils","红色:" + x + " + " +   y   + " = " + (x+y));
		
		return x+y;
	}
	/*
		 这样测试不对
		 If you would like to submit a bug report, please visit:
   		http://java.sun.com/webapps/bugreport/crash.jsp#
	 * */
	/*
	public static void main(String[] args) {
		
		MathUtils m = new MathUtils();
		int result = m.incrment(1,2);
		System.out.println(result);
	}
	*/
	
}
</span>

3.
 <pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ithema28.junittest"
    android:versionCode="1"
    android:versionName="1.0" >


<!--     指定测试信息和要测试的包 -->
    <instrumentation android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.ithema28.junittest"></instrumentation>
    
    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />
    
    <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=".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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值