一、单元测试:
1、Android单元测试的利器:Juint(一):Juint的介绍和基础使用
2、Android单元测试的利器:Juint(二):Juint的生命周期
3、Android单元测试的利器:Juint(三):Juint的详细用法(上)
二、Android测试(Instrumentation测试)
使用Android Studio 3.0新建工程,使用Android视图在java目录下会有三个目录,一个是android的source,一个带(test),一个带(android Test),带(android Test)的是用于Instrumentation测试的。
1、引用库:
android {
defaultConfig {
……
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
……
}
}
dependencies {
……
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestCompile 'com.android.support.test:rules:1.0.2'
……
}
2、测试:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:text="Hello" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:hint="Enter your name here"
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Say hello!"
android:layout_below="@+id/editText"
android:onClick="sayHello"/>
</RelativeLayout>
@RunWith(AndroidJUnit4.class)
public class MainActivityInstrumentedTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
MainActivity.class);
private static final String STRING_TO_BE_TYPED = "Peter";
@Test
public void sayHello(){
onView(withId(R.id.editText)).perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard()); //line 1
onView(withText("Say hello!")).perform(click()); //line 2
String expectedText = "Hello, " + STRING_TO_BE_TYPED + "!";
onView(withId(R.id.textView)).check(matches(withText(expectedText))); //line 3
}
}
3、相关api:
https://blog.csdn.net/qiang_xi/article/details/52933593
更新中……