zoukankan      html  css  js  c++  java
  • #单元测试

    1.环境配置:

    //add for test
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    //add for test
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
    }

    2.界面布局:

    ...


    <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" />
    <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="SAY HFLLO"
    android:onClick="sayHello"/>


    java代码:

    public class mainActivity extends AppCompatActivity {
    private TextView textView;
    private EditText editText;
    private Button btn;

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

    }

    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()+"!");
    }
    }

     测试:

    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() throws Exception {
                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)));

        }

    }

     

  • 相关阅读:
    知社 —— 第 4 次站立式会议(04-28)
    知社 —— 第 3 次站立式会议(04-27)
    知社 —— 第 2 次站立式会议(04-26)
    知社 —— 第 1 次站立式会议(04-25)
    团队代码规范、冲刺任务与计划
    团队作业第四次 — 项目系统设计与数据库设计
    团队作业第三次 — 项目需求分析
    第01组 Alpha冲刺(5/6)
    2019 SDN上机第4次作业
    2019 SDN阅读作业
  • 原文地址:https://www.cnblogs.com/07zhouzijing/p/6582325.html
Copyright © 2011-2022 走看看