Android搭建Junit测环境

发表于:2016-4-19 10:22

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:蚂蚁1    来源:51Testing软件测试网采编

  在AndroidManifest.xml文件中增加两个东西,分别是:
  1、uses-library ,位于application里面。
  2、instrumentation,与application同级。
  完整的AndroidManifest.xml文件如下:
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开头。
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。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号