Hướng dẫn này trình bày cách cài đặt Espresso bằng Trình quản lý SDK và tạo Espresso bằng Gradle. Bạn nên sử dụng Android Studio.
Thiết lập môi trường kiểm thử
Để tránh kết quả không ổn định, bạn nên tắt ảnh động hệ thống trên các thiết bị thực hoặc ảo dùng để kiểm thử. Trên thiết bị, trong phần Cài đặt > Tuỳ chọn cho nhà phát triển, hãy tắt 3 chế độ cài đặt sau:
- Tỷ lệ hình động của cửa sổ
- Tỷ lệ hình động chuyển tiếp
- Tỷ lệ thời lượng của trình tạo hình động
Thêm phần phụ thuộc Espresso
Để thêm các phần phụ thuộc Espresso vào dự án, hãy hoàn tất các bước sau:
- Mở tệp
build.gradle
của ứng dụng. Đây thường không phải là tệpbuild.gradle
cấp cao nhất mà làapp/build.gradle
. - Thêm các dòng sau bên trong phần phụ thuộc:
Groovy
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' androidTestImplementation 'androidx.test:runner:1.4.0' androidTestImplementation 'androidx.test:rules:1.4.0'
Kotlin
androidTestImplementation('androidx.test.espresso:espresso-core:3.4.0') androidTestImplementation('androidx.test:runner:1.4.0') androidTestImplementation('androidx.test:rules:1.4.0')
Xem tập hợp hoàn chỉnh các phần phụ thuộc Gradle.
Đặt trình chạy đo lường
Thêm vào chính tệp build.gradle
dòng sau trong android.defaultConfig
:
Groovy
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Kotlin
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Ví dụ về tệp bản dựng Gradle
Groovy
plugins { id 'com.android.application' } android { compileSdkVersion 33 defaultConfig { applicationId "com.my.awesome.app" minSdkVersion 21 targetSdkVersion 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } } dependencies { androidTestImplementation 'androidx.test:runner:1.4.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' }
Kotlin
plugins { id("com.android.application") } android { compileSdkVersion(33) defaultConfig { applicationId = "com.my.awesome.app" minSdkVersion(21) targetSdkVersion(33) versionCode = 1 versionName = "1.0" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } } dependencies { androidTestImplementation('androidx.test:runner:1.4.0') androidTestImplementation('androidx.test.espresso:espresso-core:3.4.0') }
Số liệu phân tích
Để đảm bảo chúng tôi đi đúng hướng với mỗi bản phát hành mới, trình chạy kiểm thử sẽ thu thập số liệu phân tích. Cụ thể hơn, hàm này sẽ tải hàm băm tên gói của ứng dụng đang được kiểm thử lên cho mỗi lệnh gọi. Điều này cho phép chúng tôi đo lường cả số lượng gói riêng biệt bằng Espresso cũng như khối lượng sử dụng.
Nếu không muốn tải dữ liệu này lên, bạn có thể chọn không tải bằng cách thêm đối số disableAnalytics
vào lệnh đo lường:
adb shell am instrument -e disableAnalytics true
Thêm chương trình kiểm thử đầu tiên
Theo mặc định, Android Studio sẽ tạo các hoạt động kiểm thử trong src/androidTest/java/com.example.package/
.
Ví dụ về kiểm thử JUnit4 sử dụng Quy tắc:
Kotlin
@RunWith(AndroidJUnit4::class) @LargeTest class HelloWorldEspressoTest { @get:Rule val activityRule = ActivityScenarioRule(MainActivity::class.java) @Test fun listGoesOverTheFold() { onView(withText("Hello world!")).check(matches(isDisplayed())) } }
Java
@RunWith(AndroidJUnit4.class) @LargeTest public class HelloWorldEspressoTest { @Rule public ActivityScenarioRule<MainActivity> activityRule = new ActivityScenarioRule<>(MainActivity.class); @Test public void listGoesOverTheFold() { onView(withText("Hello world!")).check(matches(isDisplayed())); } }
Chạy chương trình kiểm thử
Bạn có thể chạy kiểm thử trong Android Studio hoặc qua dòng lệnh.
Trong Android Studio
Để tạo một cấu hình kiểm thử trong Android Studio, hãy hoàn tất các bước sau:
- Mở Run > Edit Configurations (Chạy > Chỉnh sửa cấu hình).
- Thêm một cấu hình Kiểm thử Android mới.
- Chọn một mô-đun.
- Thêm một trình chạy đo lường cụ thể:
androidx.test.runner.AndroidJUnitRunner
- Chạy cấu hình mới tạo.
Từ dòng lệnh
Thực thi lệnh Gradle sau:
./gradlew connectedAndroidTest