-
首先创建项目,放入所需被测试类,然后开始配置gradle依赖项。
-
打开应用的 build.gradle (project)文件
-
在 repositories 部分中,确保显示了 Google’s Maven 代码库:
allprojects {
repositories {
jcenter()
google()
}
} -
在build.gradle(Moudle)文件中添加所需要的软件包(不用全部添加,选自己测试要用的添加就行)。
dependencies {
// Core library
androidTestImplementation ‘androidx.test:core:1.0.0’// AndroidJUnitRunner and JUnit Rules
androidTestImplementation ‘androidx.test🏃1.1.0’
androidTestImplementation ‘androidx.test:rules:1.1.0’// Assertions
androidTestImplementation ‘androidx.test.ext:junit:1.0.0’
androidTestImplementation ‘androidx.test.ext:truth:1.0.0’
androidTestImplementation ‘com.google.truth:truth:0.42’// Espresso dependencies
androidTestImplementation ‘androidx.test.espresso:espresso-core:3.1.0’
androidTestImplementation ‘androidx.test.espresso:espresso-contrib:3.1.0’
androidTestImplementation ‘androidx.test.espresso:espresso-intents:3.1.0’
androidTestImplementation ‘androidx.test.espresso:espresso-accessibility:3.1.0’
androidTestImplementation ‘androidx.test.espresso:espresso-web:3.1.0’
androidTestImplementation ‘androidx.test.espresso.idling:idling-concurrent:3.1.0’// The following Espresso dependency can be either “implementation”
// or “androidTestImplementation”, depending on whether you want the
// dependency to appear on your APK’s compile classpath or the test APK
// classpath.
androidTestImplementation ‘androidx.test.espresso:espresso-idling-resource:3.1.0’
}
(1)如果测试依赖于基于 JUnit 的类,请build.gradle(Moudle)在添加相应的useLibrary;例如:
(2)还需要再AndroidMainfest.xml中去添加相应的 元素(关于required这个参数,官方是这样解释的如果 你的app minSdkVersion is 28 or higher就不需要添加这个参数,否则就要添加)
相应的useLibrary请查阅这个官方地址https://developer.android.google.cn/training/testing/set-up-project#junit-based-libs
然后配置好基本就可以写测试类运行了
5. 本人运行的测试类如下:
测试方法有:testPreConditions() //测试组件是否存在
testSendKeyInts()//测试键盘事件
testCapitalizationSendingKeys() //测试大小写转换功能
testFollowlink() //测试超链接
被测类
package com.example.yidongtest_lab2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MyFirstActivity extends Activity {
private EditText mMessage;
private Button mCapitalize;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMessage = (EditText) findViewById(R.id.message1);
mCapitalize = (Button) findViewById(R.id.capitalize);
mCapitalize.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mMessage.setText(mMessage.getText().toString().toUpperCase());
}
});
}
}
被测类布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_height="wrap_content"
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:src="@drawable/ic_launcher_background"
android:layout_marginBottom="6dip"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dip">
</ImageView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/visit_home_page"
android:layout_gravity="center"
android:gravity="center"
android:textSize="24sp"
android:layout_marginTop="6dip" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/home"
android:layout_gravity="center"
android:gravity="center"
android:autoLink="web"
android:id="@+id/link"
android:textSize="18sp" />
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginBottom="0dip"
android:layout_marginLeft="6dip"
android:layout_marginRight="6dip"
android:layout_marginTop="24dip"
android:hint="@string/message_hint"
android:id="@+id/message1"
android:maxLines="1">
</EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="6dip"
android:paddingLeft="24dip"
android:paddingRight="24dip"
android:text="@string/capitalize_button"
android:id="@+id/capitalize">
</Button>
</LinearLayout>
测试用例(在Android studio自带的测试文件下创建)
package com.example.yidongtest_lab2;
import android.app.Instrumentation;
import android.content.Intent;
import android.content.IntentFilter;
import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.recyclerview.widget.ItemTouchUIUtil;
public class MyFirstActivityTest extends ActivityInstrumentationTestCase2<MyFirstActivity> {
private MyFirstActivity myFirstActivity;
private EditText mMessage;
private Button mCapitalize;
private TextView mlink;
private Instrumentation mInstrumentation;
public MyFirstActivityTest(String name){
super(MyFirstActivity.class);
setName(name);
}
public void setUp() throws Exception {
setActivityInitialTouchMode(false);
myFirstActivity=getActivity();
mInstrumentation =getInstrumentation();
mlink=(TextView)myFirstActivity.findViewById(R.id.link);
mMessage=(EditText)myFirstActivity.findViewById(R.id.message1);
mCapitalize=(Button)myFirstActivity.findViewById(R.id.capitalize);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testPreConditions(){
assertNotNull(myFirstActivity);
assertNotNull(mInstrumentation);
assertNotNull(mlink);
assertNotNull(mMessage);
assertNotNull(mCapitalize);
}
public void testSendKeyInts(){
requestMessageFocus();
sendKeys(KeyEvent.KEYCODE_H,
KeyEvent.KEYCODE_E,
KeyEvent.KEYCODE_E,
KeyEvent.KEYCODE_E,
KeyEvent.KEYCODE_Y,
KeyEvent.KEYCODE_ALT_LEFT,
KeyEvent.KEYCODE_1,
KeyEvent.KEYCODE_DPAD_DOWN,
KeyEvent.KEYCODE_ENTER);
final String expected ="heeey1\n";
final String actual=mMessage.getText().toString();
assertEquals(expected,actual);
}
private void requestMessageFocus(){
try {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
mMessage.requestFocus();
}
});
} catch (Throwable throwable) {
fail("could't set focus");
}
mInstrumentation.waitForIdleSync();
}
//测试转换大小写的功能
public void testCapitalizationSendingKeys(){
final String KeysSequence = "T E S T SPACE M E";
requestMessageFocus();
sendKeys(KeysSequence);
TouchUtils.clickView(this,mCapitalize);
final String expected ="test me".toUpperCase();
final String actual =mMessage.getText().toString();
assertEquals(expected,actual);
}
//测试超链接
public void testFollowlink(){
final Instrumentation inst =getInstrumentation();
IntentFilter intentFilter =new IntentFilter(Intent.ACTION_VIEW);
intentFilter.addDataScheme("http");
intentFilter.addCategory(Intent.CATEGORY_BROWSABLE);
Instrumentation.ActivityMonitor monitor=inst.addMonitor(intentFilter,null,false);
assertEquals(0,monitor.getHits());
TouchUtils.clickView(this,mlink);
Toast.makeText(getActivity(),"click",Toast.LENGTH_SHORT).show();
monitor.waitForActivityWithTimeout(5000);
assertEquals(1,monitor.getHits());
inst.removeMonitor(monitor);
}
}
项目运行结果: