วิธีการตั้งค่าเอสเพรสโซ
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คู่มือนี้ครอบคลุมการติดตั้ง Espresso โดยใช้ SDK Manager และการสร้างโดยใช้ Gradle
ขอแนะนำให้ใช้ Android Studio
ตั้งค่าสภาพแวดล้อมการทดสอบ
เราขอแนะนำอย่างยิ่งให้ปิดภาพเคลื่อนไหวของระบบในอุปกรณ์เสมือนหรืออุปกรณ์จริงที่ใช้สำหรับการทดสอบเพื่อหลีกเลี่ยงความไม่เสถียร ในอุปกรณ์ ให้ไปที่
การตั้งค่า > ตัวเลือกสำหรับนักพัฒนาแอป แล้วปิดการตั้งค่า 3 รายการต่อไปนี้
- อัตราการเคลื่อนไหวของหน้าต่าง
- อัตราการเคลื่อนไหวของการเปลี่ยนภาพ
- อัตราความเร็วตามตัวสร้างภาพเคลื่อนไหว
เพิ่มการอ้างอิง Espresso
หากต้องการเพิ่มการขึ้นต่อกันของ Espresso ในโปรเจ็กต์ ให้ทำตามขั้นตอนต่อไปนี้
- เปิดไฟล์
build.gradle
ของแอป โดยปกติแล้ว build.gradle
จะไม่ใช่ไฟล์ระดับบนสุด แต่จะเป็น app/build.gradle
- เพิ่มบรรทัดต่อไปนี้ภายใน dependencies
Groovy
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
androidTestImplementation 'androidx.test:runner:1.6.1'
androidTestImplementation 'androidx.test:rules:1.6.1'
Kotlin
androidTestImplementation('androidx.test.espresso:espresso-core:3.6.1')
androidTestImplementation('androidx.test:runner:1.6.1')
androidTestImplementation('androidx.test:rules:1.6.1')
ดูชุดทรัพยากร Dependency ของ Gradle ทั้งหมด
ตั้งค่าเครื่องมือเรียกใช้การทดสอบ
เพิ่มบรรทัดต่อไปนี้ในไฟล์ build.gradle
เดียวกันใน
android.defaultConfig
Groovy
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Kotlin
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
ตัวอย่างไฟล์บิลด์ 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.6.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
}
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.6.1')
androidTestImplementation('androidx.test.espresso:espresso-core:3.6.1')
}
ข้อมูลวิเคราะห์
โปรแกรมทดสอบจะรวบรวมข้อมูลวิเคราะห์เพื่อให้แน่ใจว่าเรากำลังดำเนินการในทิศทางที่ถูกต้องในแต่ละรุ่นใหม่
กล่าวคือ จะอัปโหลดแฮชของชื่อแพ็กเกจ
ของแอปพลิเคชันที่อยู่ระหว่างการทดสอบสำหรับการเรียกใช้แต่ละครั้ง ซึ่งช่วยให้เราวัดทั้งจำนวนแพ็กเกจที่ไม่ซ้ำกันโดยใช้ Espresso และปริมาณ
การใช้งานได้
หากไม่ต้องการอัปโหลดข้อมูลนี้ คุณสามารถเลือกไม่ใช้ได้โดยใส่disableAnalytics
อาร์กิวเมนต์ในคำสั่งการวัดผล
adb shell am instrument -e disableAnalytics true
เพิ่มการทดสอบแรก
Android Studio จะสร้างการทดสอบโดยค่าเริ่มต้นใน
src/androidTest/java/com.example.package/
ตัวอย่างการทดสอบ JUnit4 โดยใช้กฎ
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()));
}
}
ทำการทดสอบ
คุณเรียกใช้การทดสอบได้ใน Android Studio หรือจากบรรทัดคำสั่ง
ใน Android Studio
หากต้องการสร้างการกำหนดค่าทดสอบใน Android Studio ให้ทำตามขั้นตอนต่อไปนี้
- เปิดเรียกใช้ > แก้ไขการกำหนดค่า
- เพิ่มการกำหนดค่าการทดสอบ Android ใหม่
- เลือกโมดูล
- เพิ่มเครื่องมือเรียกใช้การทดสอบที่เฉพาะเจาะจง:
androidx.test.runner.AndroidJUnitRunner
- เรียกใช้การกำหนดค่าที่สร้างขึ้นใหม่
จากบรรทัดคำสั่ง
เรียกใช้คำสั่ง Gradle ต่อไปนี้
./gradlew connectedAndroidTest
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-08-27 UTC"],[],[],null,["This guide covers installing Espresso using the SDK Manager and building it\nusing Gradle. Android Studio is recommended.\n\nSet up your test environment\n\nTo avoid flakiness, we highly recommend that you turn off system animations on\nthe virtual or physical devices used for testing. On your device, under\n**Settings \\\u003e Developer options**, disable the following 3 settings:\n\n- Window animation scale\n- Transition animation scale\n- Animator duration scale\n\nAdd Espresso dependencies\n\nTo add Espresso dependencies to your project, complete the following steps:\n\n1. Open your app's `build.gradle` file. This is usually not the top-level `build.gradle` file but `app/build.gradle`.\n2. Add the following lines inside dependencies:\n\nGroovy \n\n```groovy\nandroidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'\nandroidTestImplementation 'androidx.test:runner:1.6.1'\nandroidTestImplementation 'androidx.test:rules:1.6.1'\n```\n\nKotlin \n\n```kotlin\nandroidTestImplementation('androidx.test.espresso:espresso-core:3.6.1')\nandroidTestImplementation('androidx.test:runner:1.6.1')\nandroidTestImplementation('androidx.test:rules:1.6.1')\n```\n\n[View the complete set of Gradle dependencies](/studio/build/dependencies).\n\nSet the instrumentation runner\n\nAdd to the same `build.gradle` file the following line in\n`android.defaultConfig`: \n\nGroovy \n\n```groovy\ntestInstrumentationRunner \"androidx.test.runner.AndroidJUnitRunner\"\n```\n\nKotlin \n\n```kotlin\ntestInstrumentationRunner = \"androidx.test.runner.AndroidJUnitRunner\"\n```\n\nExample Gradle build file \n\nGroovy \n\n```groovy\nplugins {\n id 'com.android.application'\n}\n\nandroid {\n compileSdkVersion 33\n\n defaultConfig {\n applicationId \"com.my.awesome.app\"\n minSdkVersion 21\n targetSdkVersion 33\n versionCode 1\n versionName \"1.0\"\n\n testInstrumentationRunner \"androidx.test.runner.AndroidJUnitRunner\"\n }\n}\n\ndependencies {\n androidTestImplementation 'androidx.test:runner:1.6.1'\n androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'\n}\n```\n\nKotlin \n\n```kotlin\nplugins {\n id(\"com.android.application\")\n}\n\nandroid {\n compileSdkVersion(33)\n\n defaultConfig {\n applicationId = \"com.my.awesome.app\"\n minSdkVersion(21)\n targetSdkVersion(33)\n versionCode = 1\n versionName = \"1.0\"\n\n testInstrumentationRunner = \"androidx.test.runner.AndroidJUnitRunner\"\n }\n}\n\ndependencies {\n androidTestImplementation('androidx.test:runner:1.6.1')\n androidTestImplementation('androidx.test.espresso:espresso-core:3.6.1')\n}\n```\n\nAnalytics\n\nIn order to make sure we are on the right track with each new release, the test\nrunner collects analytics. More specifically, it uploads a hash of the package\nname of the application under test for each invocation. This allows us to\nmeasure both the count of unique packages using Espresso as well as the volume\nof usage.\n\nIf you do not wish to upload this data, you can opt out by including the\n`disableAnalytics` argument in your instrumentation command: \n\n```bash\nadb shell am instrument -e disableAnalytics true\n```\n\nAdd the first test\n\nAndroid Studio creates tests by default in\n`src/androidTest/java/com.example.package/`.\n\nExample JUnit4 test using Rules: \n\nKotlin \n\n```kotlin\n@RunWith(AndroidJUnit4::class)\n@LargeTest\nclass HelloWorldEspressoTest {\n\n @get:Rule\n val activityRule = ActivityScenarioRule(MainActivity::class.java)\n\n @Test fun listGoesOverTheFold() {\n onView(withText(\"Hello world!\")).check(matches(isDisplayed()))\n }\n}\n```\n\nJava \n\n```java\n@RunWith(AndroidJUnit4.class)\n@LargeTest\npublic class HelloWorldEspressoTest {\n\n @Rule\n public ActivityScenarioRule\u003cMainActivity\u003e activityRule =\n new ActivityScenarioRule\u003c\u003e(MainActivity.class);\n\n @Test\n public void listGoesOverTheFold() {\n onView(withText(\"Hello world!\")).check(matches(isDisplayed()));\n }\n}\n```\n\nRun tests\n\nYou can run your tests in Android Studio or from the command line.\n\nIn Android Studio\n\nTo create a test configuration in Android Studio, complete the following steps:\n\n1. Open **Run \\\u003e Edit Configurations**.\n2. Add a new Android Tests configuration.\n3. Choose a module.\n4. Add a specific instrumentation runner: `androidx.test.runner.AndroidJUnitRunner`\n5. Run the newly created configuration.\n\nFrom the command line\n\nExecute the following Gradle command: \n\n```bash\n./gradlew connectedAndroidTest\n```"]]