单元测试

XML布局:

<TextView
android:text="Hello World"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"/>

<Button
android:text="Say Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_marginTop="41dp"
android:layout_below="@+id/textView2"
android:layout_alignparentStart="true"
android:onClick="Say Hello"/>


<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/textView2"
android:layout_alignparentStart="true"
android:layout_alignparentEnd="true"
android:text="huanying"/>

java代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public SayHello(View v){
TextView textView = (TextView)findViewById(R.id.textView);
EditText editText = (EditText)findViewById(R.id.editText);
TextView.setText("Hello,"+editText.getText().toString()+"!");
}


测试程序:

@Rule
public ActivityTestRule<MainActivity>mActivityRule = new ActivityTestRule<>(MainActivity.class);

@Text
public void sayHello(){
onView(withId(R.id.editText)).perform(typeText(STRING_TO_BE_TYPED),closeSoftKeyboard());
onView(withText("Say hello!")).perform(click());
String expectedText = "Hello,"+STING_TO_BE_TYPED+"!";
onView(withId(R.id.textView)).check(matches(withText(expectedText)));
}

原文地址:https://www.cnblogs.com/yzj40/p/6637997.html