结合junit + ant + emma实现Android项目单元测试代码覆盖率统计

发表于:2013-7-09 11:01

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

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

  结合junit + ant + emma实现Android项目单元测试代码覆盖率统计

  1.建立一个简单的android应用

  应用的界面如下,首先是两个id分别为value1和value2的输入框用于输入两个数字,然后是两个按钮,分别用来实现两个数的求和与求积操作,运算的结果在id为result的文本框中显示。

  整个应用只有一个Activity,主要代码如下:

public class MainActivity extends Activity {
protected static final String LOG_TAG = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText value1 = (EditText) findViewById(R.id.value1);
final EditText value2 = (EditText) findViewById(R.id.value2);
final EditText result = (EditText) findViewById(R.id.result);
Button addButton = (Button) findViewById(R.id.addValues);
addButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
int val1 = Integer.parseInt(value1.getText().toString());
int val2 = Integer.parseInt(value2.getText().toString());
Integer answer = val1 + val2;
result.setText(answer.toString());
} catch (Exception e) {
Log.e(LOG_TAG, "Failed to add numbers", e);
}
}
});
Button multiplyButton = (Button) findViewById(R.id.multiplyValues);
multiplyButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
int val1 = Integer.parseInt(value1.getText().toString());
int val2 = Integer.parseInt(value2.getText().toString());
Integer answer = val1 * val2;
result.setText(answer.toString());
} catch (Exception e) {
Log.e(LOG_TAG, "Failed to multiply numbers", e);
}
}
});
}

  2.利用JUnit基于android的扩展,从eclipse中生成应用项目对应的测试项目

  右击Demo项目名称,在弹出的菜单中依次选择Android->New Test Project,如图:

  在弹出的对话框中进行设置,如下:

  设置对应于Demo项目的测试项目名为:DemoTest

  选择测试对象为Demo项目:

  随后得到了针对Demo项目的基于JUnit在android上的扩展的单元测试框架:

  3.在测试项目中编写测试用例

  首先在com.winston.demo.test包中新建一个测试用例,操作如下:

  测试用例的设置如下:

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

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号