Android中的Junit单元测试


转载于: http://blog.youkuaiyun.com/yihuiworld

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

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.yihui.storgedatefile"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="19" />  
  10.       
  11.     <!-- 1、配置Junit单元测试工具instrumentation-->  
  12.     <!--   测试工具类 android:name:android.test.InstrumentationTestRunner,-->  
  13.     <!--   要测试哪个包里面的应用 com.yihui.storgedatefile-->  
  14.     <instrumentation   
  15.         android:name="android.test.InstrumentationTestRunner"   
  16.         android:targetPackage="com.yihui.storgedatefile"/>  
  17.       
  18.     <application  
  19.         android:allowBackup="true"  
  20.         android:icon="@drawable/ic_launcher"  
  21.         android:label="@string/app_name"  
  22.         android:theme="@style/AppTheme" >  
  23.           
  24.         <!-- 2、为Junit单元测试导入Library -->  
  25.         <uses-library android:name="android.test.runner"/>  
  26.           
  27.         <activity  
  28.             android:name="com.yihui.storgedatefile.MainActivity"  
  29.             android:label="@string/app_name" >  
  30.             <intent-filter>  
  31.                 <action android:name="android.intent.action.MAIN" />  
  32.   
  33.                 <category android:name="android.intent.category.LAUNCHER" />  
  34.             </intent-filter>  
  35.         </activity>  
  36.     </application>  
  37.   
  38. </manifest>  


2写单元测试用例

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

  1. package com.yihui.storgedatefile.test;  
  2.   
  3. import java.io.File;  
  4.   
  5. import android.content.Context;  
  6. import android.content.SharedPreferences;  
  7. import android.content.SharedPreferences.Editor;  
  8. import android.os.Environment;  
  9. import android.test.AndroidTestCase;  
  10. import android.util.Log;  
  11.   
  12. public class FileStorgeTestCase extends AndroidTestCase {  
  13.     private static final String TAG = "FileStorgeTestCase";  
  14.   
  15.       /* 测试方法,测试app应用数据存储的路径 */  
  16.     public void testGetFileDir(){  
  17.         //手机SDCard Dir  
  18.         File externalStorageDir = Environment.getExternalStorageDirectory();  
  19.         Log.i(TAG, "SD卡文件路径: " + externalStorageDir.getPath());    /*  /storage/sdcard  */  
  20.   
  21.         //手机内存卡  Dir  
  22.         File phoneStorageDir = Environment.getDataDirectory();  
  23.         Log.i(TAG, "手机内存卡文件路径: " + phoneStorageDir.getPath());  
  24.           
  25.         //手机内存卡files Dir  
  26.         File filesDir = getContext().getFilesDir();  
  27.         Log.i(TAG, "files Dir: " + filesDir.getPath());  
  28.           
  29.         //手机内存卡cache Dir  
  30.         File cacheDir = getContext().getCacheDir();  
  31.         Log.i(TAG, "cache Dir: " + cacheDir.getPath());  
  32.           
  33.         //shared_prefs Dir  
  34.         //SharedPreferences sharedPreferences = getContext().getSharedPreferences(null, Context.MODE_PRIVATE);  
  35.         SharedPreferences sharedPreferences = getContext().getSharedPreferences("MyFile", Context.MODE_PRIVATE);  
  36.         Editor edit = sharedPreferences.edit();  
  37.         edit.putString("姓名""李yi辉");  
  38.         edit.putInt("Android Level"1);  
  39.         edit.commit();  
  40.     }  
  41. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值