วิธีการตั้งค่าเอสเพรสโซ

คู่มือนี้ครอบคลุมการติดตั้ง Espresso โดยใช้ SDK Manager และการสร้างโดยใช้ Gradle ขอแนะนำให้ใช้ Android Studio

ตั้งค่าสภาพแวดล้อมการทดสอบ

เราขอแนะนำอย่างยิ่งให้ปิดภาพเคลื่อนไหวของระบบในอุปกรณ์เสมือนหรืออุปกรณ์จริงที่ใช้สำหรับการทดสอบเพื่อหลีกเลี่ยงความไม่เสถียร ในอุปกรณ์ ให้ไปที่ การตั้งค่า > ตัวเลือกสำหรับนักพัฒนาแอป แล้วปิดการตั้งค่า 3 รายการต่อไปนี้

  • อัตราการเคลื่อนไหวของหน้าต่าง
  • อัตราการเคลื่อนไหวของการเปลี่ยนภาพ
  • อัตราความเร็วตามตัวสร้างภาพเคลื่อนไหว

เพิ่มการอ้างอิง Espresso

หากต้องการเพิ่มการขึ้นต่อกันของ Espresso ในโปรเจ็กต์ ให้ทำตามขั้นตอนต่อไปนี้

  1. เปิดไฟล์ build.gradle ของแอป โดยปกติแล้ว build.gradle จะไม่ใช่ไฟล์ระดับบนสุด แต่จะเป็น app/build.gradle
  2. เพิ่มบรรทัดต่อไปนี้ภายใน 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 ให้ทำตามขั้นตอนต่อไปนี้

  1. เปิดเรียกใช้ > แก้ไขการกำหนดค่า
  2. เพิ่มการกำหนดค่าการทดสอบ Android ใหม่
  3. เลือกโมดูล
  4. เพิ่มเครื่องมือเรียกใช้การทดสอบที่เฉพาะเจาะจง: androidx.test.runner.AndroidJUnitRunner
  5. เรียกใช้การกำหนดค่าที่สร้างขึ้นใหม่

จากบรรทัดคำสั่ง

เรียกใช้คำสั่ง Gradle ต่อไปนี้

./gradlew connectedAndroidTest