Android测试------Espresso集成测试框架的使用

最近学习移动应用测试这门课,在集成测试中讲到了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(),既可以进行测试
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值