Android中的Junit单元测试
16lz
2021-12-04
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
更多相关文章
- 谷歌Android手机应用开发环境的搭建
- Android一个简单的socket通信手机间推送联系人
- 2013学Android不得不看的13个视频课程(共404个课时)
- 安卓的发展历程
- 评论:Android其实不免费
- 谷歌Android手机应用开发环境的搭建
- Android(安卓)单元测试和 UI 测试初步实践
- android基础知识17:Android设备常见问题与测试要领
- Benchmark Test On Android(安卓)Devices