转载:使用Junit对Android应用进行单元测试(二)
上一篇 / 下一篇 2010-10-29 09:18:01 / 个人分类:Android
转载自:http://tech.it168.com/a2010/1027/1118/000001118903_all.shtml
步骤12 SimpleCalc计算器中的加法测试用例
我们首先针对SimpleCalc中的加法进行测试用例的编写。这个测试用例中,会输入两个数(24和74),并测试是否其结果等于98。为了模拟在输入数字后点按钮的效果,我们使用了sendkeys方法,这个方法的优点在于可以在输入后自动将焦点切换到下一个控件上。最后,我们使用assertTrue的断言去判断实际结果是否就是等于98,代码如下:
private static final String NUMBER_24 = "2 4 ENTER ";
private static final String NUMBER_74 = "7 4 ENTER ";
private static final String ADD_RESULT = "98";
public void testAddValues() {
sendKeys(NUMBER_24);
// now on value2 entry
sendKeys(NUMBER_74);
// now on Add button
sendKeys("ENTER");
// get result
String mathResult = result.getText().toString();
assertTrue("Add result should be 98", mathResult.equals(ADD_RESULT));
}
步骤13 改进测试用例
由于每次测试时,其实都是使用同一个activity的,因此在每次测试时不需要清除旧的值,我们可以在一个sendKeys()方法中,发送一系列的输入命令,如下所示:
sendKeys(NUMBER_24 + NUMBER_74 + "ENTER");
我们测试一个小数的情况如下,看结果是否等于79.5
public void testAddDecimalValues() {
sendKeys(NUMBER_5_DOT_5 + NUMBER_74 + "ENTER");
String mathResult = result.getText().toString();
assertTrue("Add result should be " + ADD_DECIMAL_RESULT + " but was "
+ mathResult, mathResult.equals(ADD_DECIMAL_RESULT));
}
同样,我们去编写乘法的单元测试用例,这里我们继续使用sendKeys()方法,由于乘法的按钮就在加法的按钮右边,所以我们在用sendkey模拟输入了两个数后,发送“DRAD_RIGHT”的消息,就可以了。
public void testMultiplyValues() {
sendKeys(NUMBER_24+NUMBER_74+ " DPAD_RIGHT ENTER");
String mathResult = result.getText().toString();
assertTrue("Multiply result should be " + MULTIPLY_RESULT + " but was "
+ mathResult, mathResult.equals(MULTIPLY_RESULT));
}
步骤14 在模拟器中运行单元测试
运行单元测试的方法很简单,鼠标右键项目,在弹出的菜单中选择“Debug ASàAndroid JUnit Test”即可,运行结果如下两图所示:
▲
▲
其中红色的表示测试没办法通过,绿色的条状表示测试已经通过。
步骤15 Android中对屏幕显示的单元测试
在Android 的单元测试中,还可以针对界面的显示位置等进行单元测试。比如我们在Eclipse时开发采用的界面模拟器是在800*480的模式下的,但如果在其他尺寸规格的移动设备上是否能正常运行呢?这就需要对界面设置部分进行单元测试了。
我们另外创建一个单元测试用例,用前文所讲的方法新建立一个名为LayoutTests的单元测试用例,如下图:
▲
并编写如下代码:
import android.test.ActivityInstrumentationTestCase2;
import android.view.View;
import android.widget.Button;
import com.mamlambo.article.simplecalc.MainActivity;
import com.mamlambo.article.simplecalc.R;
public class LayoutTests extends ActivityInstrumentationTestCase2 {
private Button addValues;
private Button multiplyValues;
private View mainLayout;
public LayoutTests() {
super("com.mamlambo.article.simplecalc", MainActivity.class);
}
protected void setUp() throws Exception {
super.setUp();
MainActivity mainActivity = getActivity();
addValues = (Button) mainActivity.findViewById(R.id.addValues);
multiplyValues = (Button) mainActivity
.findViewById(R.id.multiplyValues);
mainLayout = (View) mainActivity.findViewById(R.id.mainLayout);
}
}
这里,分别获得了加法按钮和乘法按钮的实例。接下来,增加一个testAddButtonOnScreen
的方法,以测试按钮的位置是否正确。在这个方法中,首先你要决定屏幕的大小。有很多方
法去检测屏幕的大小,比如用getWidth()和getHeight()方法,当然在考虑尺寸时,还必须考
虑象标题栏,状态栏等所占用的位置大小。下面是其代码:
int fullWidth = mainLayout.getWidth();
int fullHeight = mainLayout.getHeight();
int[] mainLayoutLocation = new int[2];
mainLayout.getLocationOnScreen(mainLayoutLocation);
int[] viewLocation = new int[2];
addValues.getLocationOnScreen(viewLocation);
Rect outRect = new Rect();
addValues.getDrawingRect(outRect);
assertTrue("Add button off the right of the screen", fullWidth
+ mainLayoutLocation[0] > outRect.width() + viewLocation[0]);
assertTrue("Add button off the bottom of the screen", fullHeight
+ mainLayoutLocation[1] > outRect.height() + viewLocation[1]);
}
在各类尺寸的模拟器上运行,可以得到如下结果所示的测试结果:
480x800, portrait 模式 (通过)
800x480, landscape mode (失败)
320x480, portrait mode (失败)
480x320, landscape (失败)
480x854, portrait mode (通过)
854x480, landscape mode (失败)?
大家可以思考下为什么有的测试用例成功有的失败。
总结
本文讲解了如何使用junit配合Android的应用进行单元测试及详细步骤,以及如何在
Junit测试Android时的小技巧。可以看到,在设计完应用后应该编写单元测试用例,测试用
例越多和越详细,则对程序的正确性提高越有好处。
相关阅读:
- Android之父发微博驳乔布斯 双方口水战升级 (xin_晴, 2010-10-21)
- 总结1 (liz_happy, 2010-10-26)
- 谷歌兼并主管:收购Android是谷歌最成功交易 (xin_晴, 2010-10-28)
- 转载:使用Junit对Android应用进行单元测试(一) (51_51testing, 2010-10-29)
TAG: Android android JUnit Junit JUNIT 测试