Android 开发 单元测试

来源:互联网 发布:spss有mac版吗 编辑:程序博客网 时间:2024/06/13 14:57

通过新建一个单元测试project来对应用进行测试新建一个Android Test Project

Project name : xxxx(被测试应用)test

next 选择被测试应用



在src里新建一个测试类public test

test需要继承AndroidTestCase类(注意:是类而不是接口)

同时,import原应用的相关包

在测试类中写testxxx(被测试方法)方法,同时throws Exception



示例

package cn.my.junitest.test;import junit.framework.Assert;import cn.my.junitest.service;import android.test.AndroidTestCase;public class test extends AndroidTestCase {public void testsave() throws Exception{service service = new service();service.save(null);}public void testadd()throws Exception{service service = new service();int actual = service.add(1, 2);Assert.assertEquals(3, actual);}}

*test方法部分部分为测试代码,了解结构即可


运行时,关注右侧大纲视图里的test类中的各项test方法

Run as


Android JUnitTest

Eclipse会自动部署应用到模拟器,并在测试框架下运行


错误信息会在Junint和LogCat中显示


0 0
原创粉丝点击