Android开发-搭建Junit测试环境

在AndroidManifest.xml文件中增加两个东西,分别是:

1、uses-library ,位于application里面。

2、instrumentation,与application同级。

     【注】

          android:targetPackage是指要测试的应用所在的包即应用的Activity所在的包。

          android:name为用于跑TestCase的类。

 

 

完整的AndroidManifest.xml文件如下:

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.lzb.study.db"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <!-- 单元测试 -->  
  8.         <uses-library android:name="android.test.runner" />  
  9.         <activity android:name=".DBActivity"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity>  
  16.   
  17.     </application>  
  18.   
  19.     <!-- 启动测试器 -->  
  20.     <instrumentation android:name="android.test.InstrumentationTestRunner"  
  21.         android:targetPackage="com.lzb.study.db" android:label = "Tests for My App" />  
  22.   
  23. </manifest>   

 

然后,建立测试类。测试类中的测试方法必须以test开头。如:

[java]  view plain copy
  1. public class PersonServiceTest extends AndroidTestCase {  
  2.       
  3.     public void testSave() throws Exception {  
  4.         PersonService ps = new PersonService(this.getContext());  
  5.         Person person = new Person();  
  6.         person.setName("李刚");  
  7.         person.setAge((short)0);  
  8.         ps.save(person);  
  9.           
  10.     }  
  11. }  

然后在test类中,选中testcase,点击右键,选择Run As --> Android Junit Test,开始执行case。

原文链接:http://blog.csdn.net/hahalzb/article/details/6079010

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值