zoukankan      html  css  js  c++  java
  • android下测试方法及junit单元测试框架配置方法

     1.测试方法:

    根据测试是否知道源代码分为:

    1.  黑盒测试:只关心程序执行的过程 和 结果

    2.  白盒测试:根据源代码写测试方法 或者 测试用例

    根据测试粒度:

    1.方法测试:function test

    2.单元测试:unit test

    3.集成测试:intergration test

    根据测试的次数:

    1. 冒烟测试:smoke test(次数非常多,都测到冒烟烧起来了,可以用android 猴子)
    2. 压力测试:pressure test(用户访问量非常大,比如几百万)

    其中单元测试(如junit测试)是开放人员写完代码之后经常要去写的代码,

    2.写完测试代码后,查看测试代码能否正常执行:

    在Outline窗口下点击方法,

     

    对着方法点击右键—run as—Android JUnits Test

     

    报错:

     

    测试框架没有正确的配置,来到控制台(Console)发现

    junit does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library android.test.runner in its AndroidManifest.xml

    说明要配置 Instrumentation(指令集),uses-library使用的函数库 

    示例代码:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 
     3 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     4 
     5     package="com.example.junit"
     6 
     7     android:versionCode="1"
     8 
     9 android:versionName="1.0" >
    10 
    11  
    12 
    13 //指令集,要在manifest的节点下面
    14 
    15          <instrumentation
    16 
    17              android:name="android.test.InstrumentationTestRunner"//指令集的名称
    18 
    19              android:targetPackage="com.example.junit "/>//测试的目标应用程序,把package的包名写进来就可以了
    20 
    21  
    22 
    23     <uses-sdk
    24 
    25         android:minSdkVersion="19"
    26 
    27         android:targetSdkVersion="19" />
    28 
    29  
    30 
    31     <application
    32 
    33         android:allowBackup="true"
    34 
    35         android:icon="@drawable/ic_launcher"
    36 
    37         android:label="@string/app_name"
    38 
    39         android:theme="@style/AppTheme" >
    40 
    41                    <!--在application的节点下,使用的函数库 -->
    42 
    43         <uses-library android:name="android.test.runner"/>
    44 
    45         <activity
    46 
    47             android:name="com.example.junit.MainActivity"
    48 
    49             android:label="@string/app_name" >
    50 
    51             <intent-filter>
    52 
    53                 <action android:name="android.intent.action.MAIN" />
    54 
    55  
    56 
    57                 <category android:name="android.intent.category.LAUNCHER" />
    58 
    59             </intent-filter>
    60 
    61         </activity>
    62 
    63     </application>
    64 
    65  
    66 
    67 </manifest>
     1 package com.example.junit.service;
     2 
     3 public class CaleService {
     4 
     5          /**
     6 
     7           * 计算器相加的业务方法
     8 
     9           * @param x
    10 
    11           * @param y
    12 
    13           * @return
    14 
    15           */
    16 
    17          public int add(int x,int y)
    18 
    19          {
    20 
    21                    return x+y;
    22 
    23          }
    24 
    25         
    26 
    27          public static void main(String[] args)
    28 
    29          {
    30 
    31                    System.out.println("hahaha");
    32 
    33          }
    34 
    35 }
     1 package com.example.junit.test;
     2 
     3 import com.example.junit.service.CaleService;
     4 
     5 import android.test.AndroidTestCase;
     6 
     7  
     8 
     9 public class TestCalcService extends AndroidTestCase
    10 
    11 {
    12 
    13          /**
    14 
    15           * add方法的测试代码
    16 
    17           * 把异常抛给测试框架
    18 
    19           * @throws Exception
    20 
    21           */
    22 
    23          public void testAdd() throws Exception
    24 
    25          {
    26 
    27                    CaleService service = new CaleService();
    28 
    29                    int result = service.add(3,5);
    30 
    31                    assertEquals(8,result);//期待的结果
    32 
    33          }
    34 
    35 }

    测试结果:

     

    同时看到 :

     

    有绿条,表示测试通过

    假如上面的测试结果写的不是8,那么测试将不通过,显示的是红条

    综上总结:junits框架的使用步骤

    步骤一:继承extends AndroidTestCase

    步骤二:在manifest里面进行配置

    上面的清单文件配置参数其实挺难记的,记不住配置参数的简便做法:

    新建项目的时候选择新建android下的Android Test Project

     

    选择测试的项目

     

    选择测试的版本

     

    打开新建的工程的manifest文件,就可以看到配置参数了,粘贴一下即可

  • 相关阅读:
    最详细的 paypal 支付接口开发--Java版
    Java IO--NIO(二)
    Java IO--NIO(一)
    Java IO--BIO
    ForkJoinPools
    ScheduledThreadPool
    SingleThread
    CachedThreadPool
    ForkJoinPool
    WorkStealingPool
  • 原文地址:https://www.cnblogs.com/baoxiaofei/p/4142930.html
Copyright © 2011-2022 走看看