使用Junit对Android应用进行单元测试

发表于:2013-8-23 11:13

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

 作者:Alawn_Xu    来源:51Testing软件测试网采编

  在本文中,你将会学习到如何在Eclipse中创建Android JUnit单元测试工程以及在不同的条件下创建及运行自动测试用例

  准备工作

  本文假设读者已经有一定的Android基础知识,并且已经安装了Eclipse和Android SDK等开发工具。本文将指导读者如何将Android Junit框架应用到Android应用中去。本文还特别重点展示了如何测试Android中的Activity和如何识别程序中的错误。

  本文的示例代码可以在http://code.google.com/p/simple-calc-unit-testing/中下载

  步骤1 被测试的应用SimpleCalc概况

  在本文中,将以一个写好了的应用SimpleCalc简单计算器为例子进行讲解。这个简单计算器有两个功能,允许用户输入两个数并将它们相加或相乘,最后显示结果,如下图所示:


  步骤2 SimpleCalc的的界面设计

  由于应用比较简单,只占一屏,所以我们在/res/layout/main.xml中设计如下代码所示:

<linearlayout p="" <="" xmlns:android="http://schemas.android.com/apk/res/android" style="line-height: normal !important;">
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<textview p="" <="" android:layout_width="fill_parent" style="line-height: normal !important;">
android:layout_height="wrap_content" android:text="@string/hello"
android:gravity="center_horizontal" android:textSize="48px"
android:padding="12px" />
<edittext p="" <="android:id="@+id/value1" android:layout_height="wrap_content" style="line-height: normal !important;">
android:hint="@string/hint1" android:inputType="numberDecimal"
android:layout_width="fill_parent" android:textSize="48px">
<edittext p="" <="android:id="@+id/value2" android:layout_height="wrap_content" style="line-height: normal !important;">
android:hint="@string/hint2" android:inputType="numberDecimal"
android:layout_width="fill_parent" android:textSize="48px">
<framelayout p="" <="android:id="@+id/FrameLayout01" style="line-height: normal !important;">
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:padding="12px" android:background="#ff0000">
<linearlayout p="" <="" android:id="@+id/LinearLayout02" style="line-height: normal !important;">
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:background="#000000"
android:padding="4px">
<textview p="" <="" android:layout_width="wrap_content" style="line-height: normal !important;">
android:layout_height="wrap_content" android:text="@string/resultLabel"
android:textSize="48px" android:id="@+id/resultLabel">
<textview p="" <="" android:layout_width="wrap_content" style="line-height: normal !important;">
android:layout_height="wrap_content" android:id="@+id/result"
android:textSize="48px" android:textStyle="bold"
android:layout_marginLeft="16px">
<linearlayout p="" <="" android:id="@+id/LinearLayout03" style="line-height: normal !important;">
android:layout_height="wrap_content" android:layout_width="fill_parent">
<button p=" android:id="@+id/addValues" android:layout_height="wrap_content">
android:text="@string/add" android:textSize="32px"
android:layout_width="wrap_content">
<button p="" <=" android:id="@+id/multiplyValues" android:layout_height="wrap_content">
android:text="@string/multiply" android:textSize="32px"
android:layout_width="wrap_content">

  简单解析一下这个界面设计,我们使用了LinearLayout,以使得控件能在垂直方向竖向排列。界面中包括了显示标题“Unit Testing Sample”的textview,两个输入数字的edittext控件,一个FrameLayout控件中包含了一个水平的LinearLayout,在这个LinearLayout包含了一个显示结果的textview以及其提示文字“Result”,注意的是FrameLayout的背景颜色设置为红色,而LinearLayou设置成了黑色背景。

《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号