Android学习(25) -- 单元测试(JUnit Test)

来源:互联网 发布:什么是绿色版软件 编辑:程序博客网 时间:2024/04/19 08:17

测试分类

  • 黑盒测试
    • 测试逻辑业务:是通过使用整个软件或某种软件功能来严格地测试, 而并没有通过检查程序的源代码或者很清楚地了解该软件的源代码程序具体是怎样设计的。
  • 白盒测试

    • 测试逻辑方法:是通过程序的源代码进行测试而不使用用户界面
  • 根据测试粒度

    • 单元测试:unit test,针对方法来进行正确性检验的测试工作。
    • 集成测试:integration test,是在单元测试的基础上,将所有模块按照概要设计要求组装成为子系统或系统。
    • 系统测试:system test,是在所有单元、集成测试后,对系统的功能及性能的总体测试。

单元测试JUnit Test

  • 定义一个类继承AndroidTestCase,在类中定义方法,即可测试该方法

  • 在指定指令集时,targetPackage指定你要测试的应用的包名

    <instrumentation android:name="android.test.InstrumentationTestRunner"android:targetPackage="com.exp.junit"></instrumentation>
  • 定义使用的类库

    <uses-library android:name="android.test.runner"></uses-library>
  • 断言的作用,检测运行结果和预期是否一致

  • 如果应用出现异常,会抛给测试框架
  • 在项目上点击Run As—->Android Junit Test,这时候会将我们开发的app,安装到手机或者模拟器上,然后运行我们的测试用例。
  • 重写的方法

    //测试框架初始化完毕之后,在测试方法执行之前,此方法调用@Overrideprotected void setUp() throws Exception {    super.setUp();}//测试方法执行完毕之后,此方法调用@Overrideprotected void tearDown() throws Exception {    super.tearDown();}

业务代码

    package com.exp.junit.utils;    public class Utils {        public static int add(int i, int j){            return i + j;        }     }

测试代码

    package com.exp.junit.test;    import com.exp.junit.utils.Utils;    import android.test.AndroidTestCase;    public class TestCase extends AndroidTestCase {        public void test(){            int result = Utils.add(3, 5);            //断言:用来检测实际值与期望值是否一致            assertEquals(8, result);        }     }

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.exp.junit"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <instrumentation            android:name="android.test.InstrumentationTestRunner"        android:targetPackage="com.exp.junit"        ></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.exp.junit.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>
0 0
原创粉丝点击