تعليمات إعداد الإسبريسو
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يتناول هذا الدليل عملية تثبيت Espresso باستخدام "مدير حزمة تطوير البرامج" (SDK) وإنشائه باستخدام Gradle. ننصحك باستخدام "استوديو Android".
إعداد بيئة الاختبار
لتجنُّب حدوث أخطاء متقطّعة، ننصحك بشدة بإيقاف الرسوم المتحركة للنظام على الأجهزة الافتراضية أو الحقيقية المستخدَمة في الاختبار. على جهازك، ضمن
الإعدادات > خيارات المطوّرين، أوقِف الإعدادات الثلاثة التالية:
- حجم الرسوم المتحركة للنافذة
- حجم الرسوم المتحركة للنقل
- طول مدة الرسوم المتحركة
إضافة تبعيات Espresso
لإضافة تبعيات Espresso إلى مشروعك، أكمل الخطوات التالية:
- افتح ملف
build.gradle
التطبيق. عادةً ما يكون هذا الملف app/build.gradle
وليس ملف build.gradle
المستوى الأعلى.
- أضِف الأسطر التالية داخل التبعيات:
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')
عرض المجموعة الكاملة من تبعيات 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" الاختبارات تلقائيًا في
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" أو من سطر الأوامر.
في "استوديو Android"
لإنشاء إعدادات تجريبية في Android Studio، أكمِل الخطوات التالية:
- افتح تشغيل > تعديل الإعدادات.
- أضِف إعدادات جديدة لـ "اختبارات Android".
- اختَر وحدة.
- إضافة أداة تنفيذ اختبار محدّدة:
androidx.test.runner.AndroidJUnitRunner
- نفِّذ الإعدادات التي تم إنشاؤها حديثًا.
من سطر الأوامر
نفِّذ أمر Gradle التالي:
./gradlew connectedAndroidTest
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-08-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],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```"]]