前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[android] android下junit测试框架配置

[android] android下junit测试框架配置

作者头像
唯一Chat
发布2019-09-10 10:57:13
5350
发布2019-09-10 10:57:13
举报
文章被收录于专栏:陶士涵的菜地陶士涵的菜地

我们的业务代码一般是放在一个新的包下面,这个业务类不能够通过右键run as java application,因为android项目只能运行在手机上的dalvak虚拟机里面

新建一个包,里面写测试类,测试类需要继承AndroidTestCase类,写测试方法,需要throws exception抛出异常给测试框架,测试方法里面一般new出需测试的类,调用它的方法,然后断言结果,assertEquals(预估, 实际结果)

在outline视窗 (window=>show view=>outline)里面选中该方法右键run as android junit test

此时会报错

[2016-02-27 21:29:54 - 单元测试] 单元测试 does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library android.test.runner in its AndroidManifest.xml

需要在清单文件里面配置instrumentation指令集

<!-- 指令集在manifest节点下 -->

<!-- 测试用例,名称是固定的,目标包名 -->

<instrumentation

android:name="android.test.InstrumentationTestRunner"

android:targetPackage="com.tsh.junit" >

</instrumentation>

<!-- 使用的函数库,在application节点下 -->

<uses-library android:name="android.test.runner"/>

Xml里面写注释 ctrl+shift+/,

Junit里面打印出绿条,说明没有错误,如果有错误打印红色的条,错误追逐里面有错误信息,如断言错误等,如果清单文件里面的信息记不住,那么请这样操作,new => project =>android android test project => select test target 完成以后会有个项目里面有清单文件

java代码:

代码语言:javascript
复制
package com.tsh.junit.test;

import com.tsh.junit.service.CalcService;

import android.test.AndroidTestCase;

public class CalServiceTest extends AndroidTestCase {
    public void testAdd() throws Exception{
        CalcService service=new CalcService();
        int res=service.add(3, 5);
        assertEquals(8, res);
    }
}

清单文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tsh.junit"
    android:versionCode="1"
    android:versionName="1.0" >
    <!-- 指令集在manifest节点下 -->
    <!-- 测试用例,名称是固定的,目标包名 -->
    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.tsh.junit" >
    </instrumentation>

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="23" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <!-- 使用的函数库,在application节点下 -->
        <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>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-02-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档