zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

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

Androidjunit应用 进行 单元测试 使用
2023-09-11 14:20:33 时间

在本文中,你将会学习到如何在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"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
textview p="" ="" android:layout_width="fill_parent"
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"
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"
android:hint="@string/hint2" android:inputType="numberDecimal"
android:layout_width="fill_parent" android:textSize="48px"
framelayout p="" ="android:id="@+id/FrameLayout01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:padding="12px" android:background="#ff0000"
linearlayout p="" ="" android:id="@+id/LinearLayout02"
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"
android:layout_height="wrap_content" android:text="@string/resultLabel"
android:textSize="48px" android:id="@+id/resultLabel"
textview p="" ="" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/result"
android:textSize="48px" android:text
android:layout_marginLeft="16px"
linearlayout p="" ="" android:id="@+id/LinearLayout03"
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设置成了黑色背景。   



最新内容请见作者的GitHub页:http://qaseven.github.io/

   


前面一章介绍了JUnit的一些基本用法,本章来介绍关于JUnit更高级的用法,这些功能我们可能并不一定会用到,但是了解它,对JUnit会有更深刻的认识。 5.1 Test runners 大家刚开始使用JUnit的时候,可能会跟我一样有一个疑问,JUnit没有main()方法,那它是怎么开始执行的呢?众所周知,不管是什么程序,都必须有一个程序执行入口,而这个入口通常是main()方法。
JUnit是java开发人员的一个主要的测试工具,做Android开发同样是离不开java的,所以Android单元测试依然可以基于JUnit来写测试。但是JUnit只能运行在纯java环境上,前面我们介绍过MVP架构下,可以将View层隔离开来,单独针对Presenter层、Model层来测试。