Android中的Junit单元测试

Android中的Junit单元测试

在实际开发中,经常要对已经实现的功能进行单元测试,以保证当前单元没问题,尽可能的减少已有功能的bug

和Java中的开发一样,Android中对单元测试也可以采用Junit,在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确与否

 

在Android中要使用Junit单元测试,只需简单的两个步骤就可以用Junit进行单元测试应用了

1.清单文件AndroidManifest.xml中添加instrumentation工具类和uses-library

2.写一个测试类,继承自AndroidTestCase类


具体说明

1在Android的测试项目中的清单文件AndroidManifest.xml中添加instrumentation工具类和uses-library

   在AndroidManifest.xml配置instrumentation 和 uses-library

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    
    <!-- 1、配置Junit单元测试工具instrumentation-->
    <!--   测试工具类 android:name:android.test.InstrumentationTestRunner,-->
    <!--   要测试哪个包里面的应用 com.yihui.storgedatefile-->
    <instrumentation 
        android:name="android.test.InstrumentationTestRunner" 
        android:targetPackage="com.yihui.storgedatefile"/>
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
        <!-- 2、为Junit单元测试导入Library -->
        <uses-library android:name="android.test.runner"/>
        
        <activity
            android:name="com.yihui.storgedatefile.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>


2写单元测试用例

   写一个测试类,继承自AndroidTestCase类;然后写测试方法

package com.yihui.storgedatefile.test;

import java.io.File;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Environment;
import android.test.AndroidTestCase;
import android.util.Log;

public class FileStorgeTestCase extends AndroidTestCase {
	private static final String TAG = "FileStorgeTestCase";

      /* 测试方法,测试app应用数据存储的路径 */
	public void testGetFileDir(){
		//手机SDCard Dir
		File externalStorageDir = Environment.getExternalStorageDirectory();
		Log.i(TAG, "SD卡文件路径: " + externalStorageDir.getPath());	   /*  /storage/sdcard  */

		//手机内存卡  Dir
		File phoneStorageDir = Environment.getDataDirectory();
		Log.i(TAG, "手机内存卡文件路径: " + phoneStorageDir.getPath());
		
		//手机内存卡files Dir
		File filesDir = getContext().getFilesDir();
		Log.i(TAG, "files Dir: " + filesDir.getPath());
		
		//手机内存卡cache Dir
		File cacheDir = getContext().getCacheDir();
		Log.i(TAG, "cache Dir: " + cacheDir.getPath());
		
		//shared_prefs Dir
		//SharedPreferences sharedPreferences = getContext().getSharedPreferences(null, Context.MODE_PRIVATE);
		SharedPreferences sharedPreferences = getContext().getSharedPreferences("MyFile", Context.MODE_PRIVATE);
		Editor edit = sharedPreferences.edit();
		edit.putString("姓名", "李yi辉");
		edit.putInt("Android Level", 1);
		edit.commit();
	}
}



http://blog.csdn.net/yihuiworld


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值