android 测试用例编写

 

在实际开发中,开发android软件的过程需要不断地进行测试。而使用Junit测试框架,侧是正规Android开发的必用技术,在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性。

 

权限:

 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.sqliteexcercise.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>
        <uses-library android:name="android.test.runner"/>
    </application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission  android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<instrumentation android:name="android.test.InstrumentationTestRunner"   

android:targetPackage="com.example.sqliteexcercise" android:label="Tests for MyApp"/>


 

第二步:编写单元测试代码(选择要测试的方法,右键点击“Run As”--“Android Junit Test” ):

  

package cn.itcast.service;

 

publicclass PersonService{

 

    publicvoid save(String username){

       String sub = username.substring(6);

    }

   

    publicint add(int a, int b){

       return a+b;

    }

}

 

PersonServiceTest

 

package cn.itcast.junit;

 

import junit.framework.Assert;

import cn.itcast.service.PersonService;

import android.test.AndroidTestCase;

 

public class PersonServiceTest extendsAndroidTestCase {

 

       publicvoid testSave() throws Exception{

              PersonServiceservice = new PersonService();

              service.save(null);

       }

      

       publicvoid testAdd() throws Exception{

              PersonServiceservice = new PersonService();

              intactual = service.add(1, 2);

              Assert.assertEquals(3,actual);

       }

}

 

查看与输出日志信息

 

publicclass LogTest extends AndroidTestCase {

    privatestaticfinal String TAG = "LogTest";

   

    publicvoid testOutLog() throws Throwable{

       Log.i(TAG, "www.itcast.cn");

    }

   

    publicvoid testOutLog5() throws Throwable{

       Log.i(TAG, "点点滴滴");

    }

   

    publicvoid testOutLog2() throws Throwable{

       System.out.println("www.csdn.cn");

    }

   

    publicvoid testOutLog3()throws Throwable{

       System.err.println("www.sohu.cn");

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值