با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
AndroidX Test مجموعه ای از کتابخانه های Jetpack است که به شما امکان می دهد آزمایش هایی را بر روی برنامه های اندروید اجرا کنید. همچنین مجموعه ای از ابزارها را برای کمک به شما در نوشتن این تست ها فراهم می کند.
به عنوان مثال، AndroidX Test قوانین JUnit4 را برای شروع فعالیت ها و تعامل با آنها در تست های JUnit4 ارائه می دهد. همچنین شامل چارچوبهای تست UI مانند اسپرسو، UI Automator و شبیهساز Robolectric است.
کتابخانه های تست AndroidX را اضافه کنید
برای استفاده از AndroidX Test، باید وابستگی های پروژه برنامه خود را در محیط توسعه خود تغییر دهید.
وابستگی های Gradle را اضافه کنید
برای تغییر وابستگی های پروژه برنامه خود، مراحل زیر را کامل کنید:
مرحله 1 : فایل build.gradle را برای ماژول Gradle خود باز کنید.
مرحله 2 : در بخش مخازن، مطمئن شوید که مخزن Maven Google ظاهر می شود:
allprojects{repositories{jcenter()google()}}
مرحله 3 : برای هر بسته تست AndroidX که می خواهید استفاده کنید، نام بسته آن را به بخش وابستگی ها اضافه کنید. به عنوان مثال، برای افزودن بسته espresso-core ، خطوط زیر را اضافه کنید:
اینها رایج ترین وابستگی های تست AndroidX موجود هستند:
شیار
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"}
کاتلین
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"/>
ملاحظات در هنگام استفاده از کلاس های منسوخ و هدف قرار دادن Android 9 یا
بالاتر
راهنمای این بخش فقط در صورتی اعمال میشود که Android 9 (سطح API 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" />
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و 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"]]