单元测试、、、

1、activity_main中的xml中的布局
、、、

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="SAYHELLO"
    android:id="@+id/button"/>

、、、
2、MainActivity中的主代码
、、、
package com.edu.niit.ceshi;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

public void sayHello(View v) {
TextView textView = (TextView) findViewById(R.id.textView);
EditText editText = (EditText) findViewById(R.id.editText);
textView.setText("Hello," + editText.getText().toString() + "!");
}
}
、、、

3、单元测试布局程序

private static final String STRING_TO_BE_TYPED = "Peter";

@Rule
public ActivityTestRule

@Test
public void sayHello() {
onView(withId(R.id.editText))
.perform(typeText(STRING_TO_BE_TYPED),
closeSoftKeyboard());

onView(withText("Say hello!")).perform(click());

String expectedText = "Hello," + STRING_TO_BE_TYPED + "!";
onView(withId(R.id.textView))
.check(matches(withText(expectedText)));
}
、、、
单元测试结果如上、、、、、

原文地址:https://www.cnblogs.com/zzz-0613/p/6579708.html