码迷,mamicode.com
首页 > 移动开发 > 详细

Android junit单元测试

时间:2016-05-30 07:36:59      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:

软件测试的分类
* 黑盒测试
* 测试逻辑业务
* 白盒测试
* 测试逻辑方法

根据测试粒度
* 方法测试:function test
* 单元测试:unit test
* 集成测试:integration test
* 系统测试:system test

根据测试暴力程度
* 冒烟测试:smoke test
* 压力测试:pressure test


新建android项目,新建Test.java文件,注意定义一个类继承一定要继承AndroidTestCase

技术分享

package com.wuyudong.juint.test;

import com.wuyudong.juint.util.Utils;

import android.test.AndroidTestCase;

public class Test extends AndroidTestCase {
    
    public void test() {
        int res = Utils.add(3, 5);
        assertEquals(8, res);
    }
}

新建工具包文件Utils.java

package com.wuyudong.juint.util;

public class Utils {
    public static int add(int a, int b) {
        return a - b;
    }

}

运行项目,报错:

[2016-05-30 06:21:13 - 单元测试] 单元测试 does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library android.test.runner in its AndroidManifest.xml

在AndroidManifest.xml中添加下面的代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wuyudong.juint"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    
    <instrumentation 
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.wuyudong.juint"
        ></instrumentation>
    <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.wuyudong.juint.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>

继续运行单元测试test,出现下面的断言异常

技术分享

双击跳转到

public class Test extends AndroidTestCase {
    
    public void test() {
        int res = Utils.add(3, 5);
        assertEquals(8, res);
    }
}

 

Android junit单元测试

标签:

原文地址:http://www.cnblogs.com/wuyudong/p/5535580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!