AndroidTestCase来进行android 单元测试简介
1 通过AndroidTestCase 实现的大致步骤:
- 添加自己的test case code, 让它们继承自AndroidTestCase.
- 定义自己的testSuite类,用来管理test cases.
- 定义自己的testRunner,用来执行测试.
2 这种方法所涉及到的android的类以及接口:
Android Test Case classes需要从AndroidTestCase类派生出来,而不再是从junit.framework.TestCase. 二者之间的最主要区别就是Android test cases提供了一个方法getContext()来获取当前的上下文变量,这在android测试中很重要的,因为很多的android api都需要context。
2.1 AndroidTestCase主要成员:
- setUp() .
- tearDown().
- testAndroidTestCaseSetupProperly().
- TestSuite (in package junit.package):一个TestSuite就是一系列test case的集合,通过testsuite可以更好的来管理test case.
2.2TestSuite主要成员:
- void addTest (Test test).
- void addTestSuite(Class testClass).
向test suite中添加test case的方法分为如下两种:
- TestSuite suite= new TestSuite().
suite.addTest(new MathTest(“testAdd”)).
suite.addTest(new MathTest(“testDivideByZero”)). - 通过addTestSuite()来添加:
suite.addTestSuite(MathTest.class);
##2.3 TestListener
这是一个interface ,用来监听测试进程
有以下4个Public Methods :
abstract void addError(Test test,Throwable t)
abstract void addFailure(Test test,AssertionFailedError t)
abstract void endTest(Test test)
abstract void startTest(Test test)
AndroidTestRunner继承自class junit.runner.BaseTestRunner,但是它没有提供ui, 甚至来一个基于console的UI都没有,所以,如果想要很好的查看测试结果的话,你需要自己来处理来自于test runner的callback 函数。一会可以通过例子演示一下
AndroidTestRunner主要方法:
SetTest();
runTest();
addTestListener();
setContext();
3 简单导入InstrumentationTestRunner的操作步骤:
3.1 manifest.xml文件中选择Instrumentation后点击Add,如下图:
3.2 接着上一步出来的对话框中的点击name 一栏后面的Browse,如下图操作:
3.3 紧接着记得在如下图的Target package中填上你所创建的工程的包名
3.4 最后需要在 清单文件AndroidManiFest.xml中添加权限:
<uses-library android:name="android.test.runner" />
此外要检查下AndroidTestRunner 在清单文件AndroidManiFest.xml有没添加成功,有如下代码则添加成功:
<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.android_sdcard" >//所建工程的包名 </instrumentation>
4 现对sdcard的读写功能进行单元测试,代码如下:
4.1 AndroidManiFest.xml 文件 如下:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android_sdcard" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" /> //sdcard的读写权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.android_sdcard" > </instrumentation> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > //添加AndroidTestCase的权限 <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>
4.2 MainActivity .java文件如下:
package com.example.android_sdcard;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}
4.3 FileService.java文件如下:
package com.example.android_sdcard;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import org.apache.http.util.ByteArrayBuffer;import android.content.Context;import android.os.Environment;public class FileService {private Context context;public FileService(Context context) {this.context = context;}//空的构造函数public FileService() {}/**函数作用:用来读取sdcard文件的内容 * @param fileName 要读取的文件名 * @return */public String getFileFromSdcard(String fileName) {FileInputStream inputStream = null;// 缓冲的流,和磁盘无关,不需要关闭ByteArrayOutputStream outputStream = new ByteArrayOutputStream();File file = new File(Environment.getExternalStorageDirectory(),fileName);if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {try {inputStream = new FileInputStream(file);int len = 0;byte[] data = new byte[2048];while ((len = inputStream.read(data)) != -1) {outputStream.write(data, 0, len);}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if (inputStream != null) {try {inputStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}return new String(outputStream.toByteArray());}/**函数作用:向sdcard的文件写入内容 * @param fileName 文件名称 * @param content 内容 * @return */public boolean saveContentToSdcard(String fileName, String content) {boolean flag = false;FileOutputStream fileOutputStream = null;File file = new File(Environment.getExternalStorageDirectory(),fileName);if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {try {fileOutputStream = new FileOutputStream(file);fileOutputStream.write(content.getBytes());flag = true;} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if (fileOutputStream != null) {try {fileOutputStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}return flag;}}
4.4 Mytest.java文件如下:
package com.example.android_sdcard;import android.content.Context;import android.test.AndroidTestCase;import android.util.Log;public class Mytest extends AndroidTestCase {private final String TAG = "MyTest";public Mytest() {// TODO Auto-generated constructor stub}public void saveFile() {Context context = getContext();FileService fileService = new FileService();boolean flag = fileService.saveContentToSdcard("android.txt", "hello java");Log.i(TAG, "---->" + flag);}public void readFile() {Context context = getContext();FileService fileService = new FileService();String msString = fileService.getFileFromSdcard("android.txt");Log.i(TAG, "---->" + msString);}}
4.5 点在工程上右击 Run As ->Android Junit Test,结果如下:
4.6 要查看sdcard的写入内容方法:
1.如果是真机运行去查看手机sdcard下的android.txt的内容。
2.如果是模拟器运行请如下操作去查看内容:
Windows->Show View->Other->File Explorer->mnt->sdcard->android.txt 后把android.txt pull到指定磁盘位置打开查看内容。
更多相关文章
- android 混淆时出现的一些问题
- Android(安卓)丢失R.java文件处理方法
- Android进阶之如何实现国际化和各国Values文件夹命名
- (二)在Android硬件抽象层增加接口模块访问硬件驱动程序
- Android(安卓)用adb pull或push 拷贝手机文件到到电脑上
- Android登录功能实现
- android UiAutomator自定义快速调试类
- Android(安卓)引入第三方so后启动报错 /lib/arm64-v8a, /vendor/
- Android断点续传原理