android开发中junit单元测试实施步骤

1、概念介绍

junit测试就是单元测试。对于业务逻辑代码进行测试,也就是对于业务逻辑层进行测试
测试目标:方法
1)测试方法的执行是否成功。
2)测试方法的返回值是否成功。(断言测试)

2、执行步骤

执行单元测试:

1)导入测试库
2) 配置测试启动类
3)写一个类继承AndroidTestCase

1)导入测试库 配置测试启动类

<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="cn.junittest.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>
<!-- 第二步应用程序的包名-->
<instrumentation 
    android :name="android.test.InstrumentationTestRunner"
    android:targetPackage="cn.junittest">
</instrumentation>

2)写一个类继承AndroidTestCase

pub1ic class PersonServicelTest extends AndroidTestCase{
    public void testGetFirstName() throws Exception{
        PersonService ps= new PersonService() ;
        ps.getFirstName();
    }
    public void testAdd() throws Exception{ 
        PersonService ps=new PersonService();
        int actual= ps.add(1,1);
        //判断结果是否正确: 断言测试
        Assert.assertEquals(2,actual);
    }
}   

最后选择方法右击:run as –> android junit test

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值