首页 > 代码库 > 单元测试过程

单元测试过程

1、界面布局

<TextView

   android:id="@+id/textView"

   android:layout_width="wrap_content"

   android:layout_height="wrap_content"

   android:text="Hello World"/>

<EditText

    android:id="@+id/editText"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:hint="Enter your name here"/>

<Button

    android:id="@+id/button"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

   android:text="say hello"

   android:onClick="sayHello"/>

2、MainActivity.java

先声明定义:private TextView textView;private EditText  editText;private Button btn;

 public void sayHello(View view){

    TextView  textView=(TextView) findViewById(R.id.textView);

    EditText   editText=(EditText)  findViewById(R.id.editText);

    textView.setText("Hello,"+editText.getText().toString()+"!");

}

3、测试代码

 public class MainActivityTest{

      private static final String STRING_TO_BE_TYPED=""Peter;

      @Rule

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

      @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 )));

}

 

单元测试过程