最近学习移动应用测试这门课,在集成测试中讲到了Espresso,在此记录一下它的使用。
文章目录
查看设置
打开File->Settings->Appearance & Behavior->Android SDK,选择SDK tools,确保AS已经安装Android support repository
配置环境依赖
打开app->build.gradle,可以看到已经默认配置好的两个依赖
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
只需要再添加一下测试规则的依赖库
androidTestImplementation 'com.android.support.test:rules:1.0.2'
编写测试程序
新建两个活动,MainActivity和SecondActivity,在MainActivity中输入两个数字,跳转到SecondActivity后显示结果。程序如下:
MainActivity.class
public class MainActivity extends AppCompatActivity {
private EditText txt_first,txt_second;
private EditText txt_result;
private Button calculate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
void initView(){
txt_first = (EditText)findViewById(R.id.first);
txt_second = (EditText)findViewById(R.id.second);
txt_result = (EditText) findViewById(R.id.result_main);
calculate = (Button)findViewById(R.id.button);
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int a = Integer.parseInt(txt_first.getText().toString());
int b = Integer.parseInt(txt_second.getText().toString());
int c = a+b;
System.out.println("a="+a+"b="+b+"c="+c);
txt_result.setText(c+"");
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("sum",c+"");
startActivity(intent);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_marginTop="100dp"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:padding="20dp">
<EditText
android:id="@+id/first"
android:layout_width="80dp"
android:layout_height="match_parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="+"
android:textSize="50dp"/>
<EditText
android:id="@+id/second"
android:layout_width="80dp"
android:layout_height="match_parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="="
android:textSize="50dp"/>
<EditText
android:id="@+id/result_main"
android:layout_width="80dp"
android:layout_height="match_parent"/>
</LinearLayout>
<Button
android:layout_marginBottom="20dp"
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
SecondActivity.class
public class SecondActivity extends AppCompatActivity {
private TextView re;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
re = (TextView)findViewById(R.id.second_res);
Bundle data = getIntent().getExtras();
String sum = data.getString("sum");
re.setText("结果是:"+sum);
}
}
second_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/second_res"
android:layout_marginTop="200dp"
android:textSize="40dp"
android:hint="结果为:"/>
</LinearLayout>
编写测试用例
在androidTest中新建类MainActivityEspressoTest,内容如下
public class MainActivityEspressoTest {
@Rule
public ActivityTestRule<MainActivity>mainActivityActivityTestRule=
new ActivityTestRule<>(MainActivity.class);
@Test
public void changeTextNewActivity(){
//type text
onView(withId(R.id.first)).perform(typeText("1"));
onView(withId(R.id.second)).perform(typeText("1"));
//press the button
onView(withId(R.id.button)).perform(click());
//check the result
onView(withId(R.id.second_res)).check(matches(withText("结果是:2")));
}
}
测试
直接右键对应的测试方法changeTextNewActivity(),既可以进行测试