zoukankan      html  css  js  c++  java
  • 简单对App进行单元测试

    第一步:在AndroidManifest.xml中加入如下两段代码:

    1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    2.       package="com.pccw"  
    3.       android:versionCode="1"  
    4.       android:versionName="1.0">  
    5.     <uses-sdk android:minSdkVersion="8" />  
    6.   
    7.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
    8.         <activity android:name=".MainActivity"  
    9.                   android:label="@string/app_name">  
    10.             <intent-filter>  
    11.                 <action android:name="android.intent.action.MAIN" />  
    12.                 <category android:name="android.intent.category.LAUNCHER" />  
    13.             </intent-filter>  
    14.         </activity>  
    15.         <!—添加代码1-->  
    16.         <uses-library android:name="android.test.runner"/>  
    17. </application>  
    18.     <!—添加代码2-->  
    19.         <instrumentation android:name="android.test.InstrumentationTestRunner"  
    20.             android:targetPackage="com.pccw" android:label="aaa"/>  
    21. </manifest>  

    1.         <uses-library android:name="android.test.runner"/>代表把单元测试框架中的一些依赖库引入进来

    2.         <instrumentation android:name="android.test.InstrumentationTestRunner"android:targetPackage="com.pccw" android:label="aaa"/>代表配置单元测试框架的启动装置,启动装置有好几个类,可以选择,一般情况下我们使用上面这个。

    3.         targetPackage与上面的package相同,代表单元测试框架和当前应用是处于同一个进程中

    第二步:编写业务逻辑,即需要被测试的模块

    1. public class PersonService {  
    2.       
    3.     public void save(String name){  
    4.         String sub = name.substring(6);  
    5.     }  
    6.       
    7.     public int add(int a, int b){  
    8.         return a+b;  
    9.     }  
    10. }  


     

    第三步:编写单元测试代码

    1. public class PersonServiceTest extends AndroidTestCase {  
    2.       
    3.     public void testSave() throws Exception {  
    4.         PersonService service = new PersonService();  
    5.         service.save(null);  
    6.     }  
    7.       
    8.     public void testAdd() throws Exception {  
    9.         PersonService service = new PersonService();  
    10.         int result = service.add(1, 2);  
    11.         Assert.assertEquals(3, result);  
    12.     }  
    13. }  


     

    第四步:打开eclipse中的outline窗口,其中会显示单元测试类的所有的方法

    然后想要测试哪个方法,则在哪个测试方法上右键鼠标,选择Run As,然后再选择Android JUnit Test即可,如果有异常或者错误,则会出现如下情况:

    如果是正常的,则会如下:

  • 相关阅读:
    Python+requests+unittest+excel实现接口自动化测试框架
    Python+Selenium--异常截图
    如何利用Jmeter做代理录制脚本
    linux命令2
    python+selenium定位日期方法
    利用 python requests完成接口文件上传
    Python+Selenium--自动生成HTML测试报告
    代理服务器之趣谈工作原理
    python从入门到实践课后习题第九章
    python从入门到实践课后习题第十章
  • 原文地址:https://www.cnblogs.com/ymczxy/p/4723748.html
Copyright © 2011-2022 走看看