单元测试
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单元测试相关的知识,希望对你有一定的参考价值。
好吧,其实我也不怎么会,别人一边说我一边写的~~~
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)));
}
结束
以上是关于单元测试的主要内容,如果未能解决你的问题,请参考以下文章