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 DirFile externalStorageDir = Environment.getExternalStorageDirectory();Log.i(TAG, "SD卡文件路径: " + externalStorageDir.getPath());   /*  /storage/sdcard  *///手机内存卡  DirFile phoneStorageDir = Environment.getDataDirectory();Log.i(TAG, "手机内存卡文件路径: " + phoneStorageDir.getPath());//手机内存卡files DirFile filesDir = getContext().getFilesDir();Log.i(TAG, "files Dir: " + filesDir.getPath());//手机内存卡cache DirFile 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


更多相关文章

  1. 谷歌Android手机应用开发环境的搭建
  2. Android一个简单的socket通信手机间推送联系人
  3. 2013学Android不得不看的13个视频课程(共404个课时)
  4. 安卓的发展历程
  5. 评论:Android其实不免费
  6. 谷歌Android手机应用开发环境的搭建
  7. Android(安卓)单元测试和 UI 测试初步实践
  8. android基础知识17:Android设备常见问题与测试要领
  9. Benchmark Test On Android(安卓)Devices

随机推荐

  1. Google工程师解析Android系统架构
  2. Android(安卓)文件上传支持拍照录用录视
  3. Android应用开发提高系列(4)——Android动
  4. 《疯狂Android讲义》第4版 代码
  5. android新闻端demo
  6. Android(安卓)ButterKnife框架的使用方法
  7. Android下的摄像头驱动开发
  8. android ListView 九大重要属性详细分析
  9. android开发笔记之2012版辅助开发工具包(A
  10. [Android(安卓)Studio 权威教程]Windows下