基于Instrumentation的android单元测试实践
作者:网络转载 发布时间:[ 2015/9/10 16:02:53 ] 推荐标签:单元测试 移动测试
以下为源码,可参考:
MainActivity代码
package com.example.jc.instrumentsample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
private Button btn;
private TextView tv;
private EditText num1;
private EditText num2;
private EditText rs;
private Button mult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button_change);
btn.setOnClickListener(this);
tv = (TextView) findViewById(R.id.textView_hello);
num1 =(EditText)findViewById(R.id.editText_num1);
num2 =(EditText)findViewById(R.id.editText_num2);
rs = (EditText)findViewById(R.id.editText_rs);
mult = (Button)findViewById(R.id.button_multip);
mult.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button_change:
changeTextView();
break;
case R.id.button_multip:
computeMultip();
default:
break;
}
}
//乘法
private void computeMultip() {
double n1 = Double.parseDouble(num1.getText().toString());
double n2 = Double.parseDouble(num2.getText().toString());
double res = n1*n2;
rs.setText(String.valueOf(res));
}
// textView变换
private void changeTextView() {tv.setText("this is my first android");}
// 加法
public int add(int a, int b) {return a + b;}
//减法
public int sub(int a, int b) {return b - a;}
}
sampleTest
package com.example.jc.instrumentsample;
import android.content.Intent;
import android.os.SystemClock;
import android.test.InstrumentationTestCase;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* Created by jc on 15/9/1.
*/
public class sampleTest extends InstrumentationTestCase {
private MainActivity sample = null;
private Button btn = null;
private TextView textView = null;
private EditText num1 = null;
private EditText num2 = null;
private EditText rs = null;
private Button mult = null;
@Override
protected void setUp() throws Exception {
try {
Log.d("1111","111");
super.setUp();
}catch (Exception e){
e.printStackTrace();
}
Intent intent = new Intent();
intent.setClassName("com.example.jc.instrumentsample", MainActivity.class.getName());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sample = (MainActivity)getInstrumentation().startActivitySync(intent);
btn = (Button)sample.findViewById(R.id.button_change);
textView = (TextView)sample.findViewById(R.id.textView_hello);
num1 = (EditText)sample.findViewById(R.id.editText_num1);
num2 = (EditText)sample.findViewById(R.id.editText_num2);
rs = (EditText)sample.findViewById(R.id.editText_rs);
mult = (Button)sample.findViewById(R.id.button_multip);
}
@Override
protected void tearDown() throws Exception {
sample.finish();
try {
super.tearDown();
}catch (Exception e){
e.printStackTrace();
}
}
public void testMultip(){
SystemClock.sleep(1000);
getInstrumentation().runOnMainSync(new PerFromClickMultip(mult));
SystemClock.sleep(1000);
assertEquals("210.0", rs.getText().toString());
}
private class PerFromClickMultip implements Runnable {
private Button m = null;
public PerFromClickMultip(Button mult) {
m = mult;
}
@Override
public void run() {
num1.setText(String.valueOf(21));
num2.setText(String.valueOf(10));
double n1 = Double.parseDouble(num1.getText().toString());
double n2 = Double.parseDouble(num2.getText().toString());
double n3 = n1* n2;
rs.setText(String.valueOf(n3));
}
}
public void testActivity(){
Log.d("testActivity","testActivity");
SystemClock.sleep(1500);
getInstrumentation().runOnMainSync(new PerFromClick(btn));
SystemClock.sleep(1500);
assertEquals("hello andriod",textView.getText().toString());
}
private class PerFromClick implements Runnable {
Button btn2;
public PerFromClick(Button button){
btn2 = button;
}
@Override
public void run() {
btn2.performClick();
}
}
public void testAdd(){
String tag = "testAdd";
Log.d("testActivity","testAdd");
int test = sample.add(1,1);
assertEquals(3,test);
}
public void testSub(){
String tag = "testSub";
Log.d("testActivity","testAdd");
int test = sample.sub(2,1);
assertEquals(1,test);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jc.instrumentsample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<uses-library android:name="android.test.runner" />
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.jc.instrumentsample" />
</manifest>
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
Android自动化测试框架有哪些?有什么用途?Android测试中最容易忽略的测试点有哪些?Android 手机自动化测试工具有哪几种?移动APP测试之android性能测试快速提升Android App 的代码覆盖率Android Unit Test 框架比较Android单元测试框架Robolectric3.0介绍(一)Android单元测试的整理在Android Studio中实现单元测试Android连接MySQL方法,测试成功关于Android MVP模式的思考Android 数据库管理?ActiveAndroid编写Android测试单元该做的和不该做的事Android阿里面试Java基础锦集在Android项目中使用Java8Java / Android 面试中所遇到的那些坑
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11热门文章
常见的移动App Bug??崩溃的测试用例设计如何用Jmeter做压力测试QC使用说明APP压力测试入门教程移动app测试中的主要问题jenkins+testng+ant+webdriver持续集成测试使用JMeter进行HTTP负载测试Selenium 2.0 WebDriver 使用指南