码迷,mamicode.com
首页 > 其他好文 > 详细

单元测试

时间:2017-03-30 00:40:53      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:pac   ttext   private   edittext   widget   ott   tco   support   ntop   

 

好吧,其实我也不怎么会,别人一边说我一边写的~~~

1、首先是配置环境

如果本身配置高,会自动的配置测试环境

2、xml布局
<TextView

    android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello Word"
android:id="@+id/textView" />


<EditText

android:hint="Enter your name here"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_alignBaseline="@+id/button"
android:layout_alignBottom="@+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<Button

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Say Hello"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="51dp" />

3、MainActivity

package com.example.lenovo.myapplication1;


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

public class MainActivity extends AppCompatActivity {

private TextView textView;

private EditText editText;

private Button button;



@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);



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

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

button= (Button) findViewById(R.id.button);



button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

editText.setText(textView.getText().toString());

}

});

}

}

4、测试程序

private static final String STRING_TO_BE_TYPED = "Peter";

、、、
package com.example.lenovo.myapplication1;

import org.junit.Rule;
import org.junit.Test;

/**
* Created by lenovo on 2017-3-16.
*/
@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)));
}
结束

单元测试

标签:pac   ttext   private   edittext   widget   ott   tco   support   ntop   

原文地址:http://www.cnblogs.com/1502720106-HZH/p/6642246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!