随笔 - 1357  文章 - 0  评论 - 1104  阅读 - 1941万

四、对应用进行单元测试

在实际开发中,开发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.ljq.activity"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <uses-library android:name="android.test.runner" />
        <activity android:name=".FileActivity"
                  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>
    <uses-sdk android:minSdkVersion="7" />
    <instrumentation android:name="android.test.InstrumentationTestRunner"
            android:targetPackage="com.ljq.activity" android:label="Tests for My App" />
</manifest>

上面targetPackage指定的包要和应用的package相同。

        

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

package com.ljq.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

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

/**
 * android测试
 *
 * @author jiqinlin
 *
 */
public class FileServiceTest extends AndroidTestCase {
    private final String TAG = "FileServiceTest";
    public void testSave() throws Exception{
        OutputStream outputStream = this.getContext().openFileOutput("ljq.txt", Context.MODE_PRIVATE);
        FileService.save(outputStream, "abc");

        Log.i(TAG, "保存成功");

    }

}

posted on   Ruthless  阅读(1408)  评论(1编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
< 2011年5月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 1 2 3 4
5 6 7 8 9 10 11

点击右上角即可分享
微信分享提示