تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
AndroidX Test هي مجموعة من مكتبات Jetpack تتيح لك إجراء اختبارات على تطبيقات Android. كما يوفّر سلسلة من الأدوات لمساعدتك في كتابة هذه الاختبارات.
على سبيل المثال، يوفّر AndroidX Test قواعد JUnit4 لبدء الأنشطة والتفاعل معها في اختبارات JUnit4. ويحتوي أيضًا على أُطر عمل لاختبار واجهة المستخدم، مثل Espresso وUI Automator ومحاكي Robolectric.
إضافة مكتبات AndroidX Test
لاستخدام AndroidX Test، يجب تعديل التبعيات في مشروع تطبيقك
ضمن بيئة التطوير.
إضافة تبعيات Gradle
لتعديل التبعيات في مشروع تطبيقك، أكمِل الخطوات التالية:
الخطوة 1: افتح ملف build.gradle لوحدة Gradle.
الخطوة 2: في قسم المستودعات، تأكَّد من ظهور مستودع Maven الخاص بـ Google:
allprojects{repositories{jcenter()google()}}
الخطوة 3: أضِف اسم حزمة كل حزمة AndroidX Test تريد استخدامها إلى قسم التبعيات. على سبيل المثال، لإضافة حزمة espresso-core، أضِف الأسطر التالية:
في ما يلي تبعيات AndroidX Test الأكثر شيوعًا المتاحة:
Groovy
dependencies{// Core libraryandroidTestImplementation"androidx.test:core:$androidXTestVersion0"// AndroidJUnitRunner and JUnit RulesandroidTestImplementation"androidx.test:runner:$testRunnerVersion"androidTestImplementation"androidx.test:rules:$testRulesVersion"// AssertionsandroidTestImplementation"androidx.test.ext:junit:$testJunitVersion"androidTestImplementation"androidx.test.ext:truth:$truthVersion"// Espresso dependenciesandroidTestImplementation"androidx.test.espresso:espresso-core:$espressoVersion"androidTestImplementation"androidx.test.espresso:espresso-contrib:$espressoVersion"androidTestImplementation"androidx.test.espresso:espresso-intents:$espressoVersion"androidTestImplementation"androidx.test.espresso:espresso-accessibility:$espressoVersion"androidTestImplementation"androidx.test.espresso:espresso-web:$espressoVersion"androidTestImplementation"androidx.test.espresso.idling:idling-concurrent:$espressoVersion"// 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:$espressoVersion"}
Kotlin
dependencies{// Core libraryandroidTestImplementation("androidx.test:core:$androidXTestVersion")// AndroidJUnitRunner and JUnit RulesandroidTestImplementation("androidx.test:runner:$testRunnerVersion")androidTestImplementation("androidx.test:rules:$testRulesVersion")// AssertionsandroidTestImplementation("androidx.test.ext:junit:$testJunitVersion")androidTestImplementation("androidx.test.ext:truth:$truthVersion")// Espresso dependenciesandroidTestImplementation("androidx.test.espresso:espresso-core:$espressoVersion")androidTestImplementation("androidx.test.espresso:espresso-contrib:$espressoVersion")androidTestImplementation("androidx.test.espresso:espresso-intents:$espressoVersion")androidTestImplementation("androidx.test.espresso:espresso-accessibility:$espressoVersion")androidTestImplementation("androidx.test.espresso:espresso-web:$espressoVersion")androidTestImplementation("androidx.test.espresso.idling:idling-concurrent:$espressoVersion")// 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:$espressoVersion")}
تحتوي صفحة ملاحظات الإصدار على جدول يتضمّن أحدث الإصدارات لكل عنصر.
يمكنك الرجوع إلى فهرس الحِزم أو فهرس الفئات للحصول على مستندات مرجعية محدّدة حول هذه المكتبات.
المشاريع التي تستخدم فئات متوقّفة نهائيًا
إذا كان تطبيقك يستخدم اختبارات تعتمد على فئات android.test
قديمة مستندة إلى JUnit3، مثل InstrumentationTestCase وTestSuiteLoader، أضِف الأسطر التالية في القسم android من الملف:
لتشغيل الاختبارات التي تعتمد على فئات android.test المستندة إلى JUnit3 المتوقّفة نهائيًا، أضِف عناصر <uses-library> اللازمة إلى بيان تطبيق الاختبار. على سبيل المثال، إذا أضفت اختبارات تعتمد على مكتبة android.test.runner، أضِف العنصر التالي إلى بيان تطبيقك:
<!--Youdon't need to include android:required="false" if your app'sminSdkVersionis28orhigher.-->
<uses-libraryandroid:name="android.test.runner"android:required="false"/>
اعتبارات عند استخدام فئات تم إيقافها واستهداف الإصدار 9 من نظام التشغيل Android أو إصدار أحدث
أعلى من درجة الحرارة المقترَحة
لا تنطبق الإرشادات الواردة في هذا القسم إلا إذا كنت تستهدف الإصدار Android 9 (المستوى 28 من واجهة برمجة التطبيقات)
أو إصدارًا أحدث وتم ضبط الحد الأدنى لإصدار حزمة تطوير البرامج (SDK) لتطبيقك على Android 9.
تعتمد مكتبة android.test.runner ضمنيًا على مكتبتَي android.test.base وandroid.test.mock. إذا كان تطبيقك يستخدم الفئات من android.test.base أو android.test.mock فقط، يمكنك تضمين المكتبات بمفردها:
<!--Forbothofthesedeclarations,youdon't need to include android:required="false" if your app'sminSdkVersionis28orhigher.-->
<uses-libraryandroid:name="android.test.base"android:required="false"/>
<uses-library android:name="android.test.mock" android:required="false" />
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-08-08 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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-08 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["# Set up project for AndroidX Test\n\nAndroidX Test is a collection of Jetpack libraries that lets you run tests\nagainst Android apps. It also provides a series of tools to help you write these\ntests.\n\nFor example, AndroidX Test provides JUnit4 rules to start activities and\ninteract with them in JUnit4 tests. It also contains UI Testing frameworks such\nas Espresso, UI Automator and the Robolectric simulator.\n\nAdd AndroidX Test libraries\n---------------------------\n\nIn order to use AndroidX Test, you must modify your app project's dependencies\nwithin your development environment.\n\n### Add Gradle dependencies\n\nTo modify your app project's dependencies, complete the following steps:\n\n- **Step 1** : Open the `build.gradle` file for your Gradle module.\n- **Step 2**: In the repositories section, make sure Google's Maven repository appears:\n\n allprojects {\n repositories {\n jcenter()\n google()\n }\n }\n\n- **Step 3** : For each AndroidX Test package you want to use, add its package name to the dependencies section. For example, to add the `espresso-core` package, add the following lines:\n\n### Groovy\n\n```groovy\ndependencies {\n ...\n androidTestImplementation \"androidx.test.espresso:espresso-core:$espressoVersion\"\n }\n```\n\n### Kotlin\n\n```kotlin\ndependencies {\n ...\n androidTestImplementation('androidx.test.espresso:espresso-core:$espressoVersion')\n }\n```\n\nThese are the most common AndroidX Test dependencies available: \n\n### Groovy\n\n```groovy\ndependencies {\n // Core library\n androidTestImplementation \"androidx.test:core:$androidXTestVersion0\"\n\n // AndroidJUnitRunner and JUnit Rules\n androidTestImplementation \"androidx.test:runner:$testRunnerVersion\"\n androidTestImplementation \"androidx.test:rules:$testRulesVersion\"\n\n // Assertions\n androidTestImplementation \"androidx.test.ext:junit:$testJunitVersion\"\n androidTestImplementation \"androidx.test.ext:truth:$truthVersion\"\n\n // Espresso dependencies\n androidTestImplementation \"androidx.test.espresso:espresso-core:$espressoVersion\"\n androidTestImplementation \"androidx.test.espresso:espresso-contrib:$espressoVersion\"\n androidTestImplementation \"androidx.test.espresso:espresso-intents:$espressoVersion\"\n androidTestImplementation \"androidx.test.espresso:espresso-accessibility:$espressoVersion\"\n androidTestImplementation \"androidx.test.espresso:espresso-web:$espressoVersion\"\n androidTestImplementation \"androidx.test.espresso.idling:idling-concurrent:$espressoVersion\"\n\n // The following Espresso dependency can be either \"implementation\",\n // or \"androidTestImplementation\", depending on whether you want the\n // dependency to appear on your APK'\"s compile classpath or the test APK\n // classpath.\n androidTestImplementation \"androidx.test.espresso:espresso-idling-resource:$espressoVersion\"\n}\n```\n\n### Kotlin\n\n```kotlin\ndependencies {\n // Core library\n androidTestImplementation(\"androidx.test:core:$androidXTestVersion\")\n\n // AndroidJUnitRunner and JUnit Rules\n androidTestImplementation(\"androidx.test:runner:$testRunnerVersion\")\n androidTestImplementation(\"androidx.test:rules:$testRulesVersion\")\n\n // Assertions\n androidTestImplementation(\"androidx.test.ext:junit:$testJunitVersion\")\n androidTestImplementation(\"androidx.test.ext:truth:$truthVersion\")\n\n // Espresso dependencies\n androidTestImplementation( \"androidx.test.espresso:espresso-core:$espressoVersion\")\n androidTestImplementation( \"androidx.test.espresso:espresso-contrib:$espressoVersion\")\n androidTestImplementation( \"androidx.test.espresso:espresso-intents:$espressoVersion\")\n androidTestImplementation( \"androidx.test.espresso:espresso-accessibility:$espressoVersion\")\n androidTestImplementation( \"androidx.test.espresso:espresso-web:$espressoVersion\")\n androidTestImplementation( \"androidx.test.espresso.idling:idling-concurrent:$espressoVersion\")\n\n // The following Espresso dependency can be either \"implementation\",\n // or \"androidTestImplementation\", depending on whether you want the\n // dependency to appear on your APK\"s compile classpath or the test APK\n // classpath.\n androidTestImplementation( \"androidx.test.espresso:espresso-idling-resource:$espressoVersion\")\n}\n```\n\nThe [Release Notes](/jetpack/androidx/releases/test) page contains a table with the latest versions per\nartifact.\n| **Note:** It's important to ensure these testing dependencies point to the correct source set. Usually AndroidX Test is needed in instrumentation tests only, so you would use `androidTestImplementation()`. However, in cases such as with `espresso-idling-resource`, the APIs are used from production code, requiring you to use the implementation function.\n\nRefer to the [Package Index](/reference/androidx/test/packages) or [Class Index](/reference/androidx/test/classes) for specific reference\ndocumentation on these libraries.\n\nProjects using deprecated classes\n---------------------------------\n\n| **Warning:** If you build instrumentation tests using Gradle, you receive additional support. When auto-generating the test manifest, the Android Gradle Plugin adds the following libraries and manifest elements to your project automatically so you don't need to take these steps.\n\nIf your app uses tests that rely on deprecated JUnit3-based `android.test`\nclasses , such as [`InstrumentationTestCase`](/reference/android/test/InstrumentationTestCase) and [`TestSuiteLoader`](/reference/junit/runner/TestSuiteLoader), add\nthe following lines in the `android` section of the file: \n\n android {\n ...\n useLibrary 'android.test.runner'\n\n useLibrary 'android.test.base'\n useLibrary 'android.test.mock'\n }\n\n| **Note:** You only need to include the libraries that contain the classes used in your app. For a list of the classes that appear in each library, see [JUnit-based libraries](/training/testing/instrumented-tests/androidx-test-libraries/test-setup).\n\n### Add manifest declarations\n\nTo run tests that rely on deprecated JUnit3-based `android.test` classes, add\nthe necessary [`\u003cuses-library\u003e`](/guide/topics/manifest/uses-library-element) elements to your test app's manifest. For\nexample, if you add tests that depend on the `android.test.runner` library, add\nthe following element to your app's manifest: \n\n \u003c!-- You don't need to include android:required=\"false\" if your app's\n\n minSdkVersion is 28 or higher. --\u003e\n\n \u003cuses-library android:name=\"android.test.runner\"\n\n android:required=\"false\" /\u003e\n\nTo determine the library that contains a given JUnit-based class, see\n[JUnit-based libraries](/training/testing/instrumented-tests/androidx-test-libraries/test-setup).\n| **Caution:** Avoid deprecated classes where possible. Use supported JUnit-based classes in their place.\n\n#### Considerations when using deprecated classes and targeting Android 9 or\n\nhigher\n\nThe guidance in this section applies only if you target Android 9 (API level 28)\nor higher *and* the minimum SDK version for your app is set to Android 9.\n\nThe `android.test.runner` library implicitly depends on the `android.test.base`\nand `android.test.mock` libraries. If your app only uses classes from\n`android.test.base` or `android.test.mock`, you can include the libraries by\nthemselves: \n\n \u003c!-- For both of these declarations, you don't need to include\n android:required=\"false\" if your app's minSdkVersion is 28\n or higher. --\u003e\n\n \u003cuses-library android:name=\"android.test.base\"\n android:required=\"false\" /\u003e\n \u003cuses-library android:name=\"android.test.mock\"\n android:required=\"false\" /\u003e"]]