安卓中如何对应用进行单元测试

在实际开发中,开发android软件的过程需要不断地进行测试。使用Junit测试框架,是正规Android开发的必用技术,在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性。

第一步:首先在AndroidManifest.xml中加入下面红色代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   
package="com.itfom.file"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="android.test.runner" />

        <activity
            android:name="com.itfom.file.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>

   
<instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:label="Tests for My App"
        android:targetPackage="com.itfom.file" />


</manifest>

第二步:编写单元测试代码(选择要测试的方法,右键点击“Run As”--“Android Junit Test” ):

package com.itfom.file;

import java.io.OutputStream;

import android.content.Context;
import android.test.AndroidTestCase;
import android.util.Log;

public class TestJunit extends AndroidTestCase {
	private final String TAG = "FileServiceTest"; 
	public void testSave() throws Exception{
		OutputStream outputStream = this.getContext().openFileOutput("xsc.txt", Context.MODE_PRIVATE);
		FileTool.saveDate(outputStream, "abc");
		Log.i(TAG, "保存成功");
	}
}

在这里我就不再给出类中定义的方法了,就是被测试的方法,saveDate(),用户在测试的时候,可以写一个简单的方法比如实现两个数的加减即可。

如果运行的结果是绿色,说明测试通过,该方法没有任何问题。如果为红色,说明该方法存在问题。上面也会给出相应的提示。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BruceCheng夏夏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值