• Android junit单元测试


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

    根据测试粒度
    * 方法测试: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);
        }
    }
  • 相关阅读:
    【shell】sed指定追加模式空间的次数
    【shell】sed后向引用替换文本
    【c++】一道关于继承和析构的笔试题
    【curl】cookie的分隔符
    【shell】grep使用正则表达式
    【leetcode】Remove Duplicates from Sorted Array
    【shell】awk格式对齐文本
    【shell】sed处理多行合并
    【leetcode】Permutations
    BWSAP BW Performance Tuning URLS LIST
  • 原文地址:https://www.cnblogs.com/wuyudong/p/5535580.html
Copyright © 2020-2023  润新知