دستورالعمل تنظیم اسپرسو
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
این راهنما نصب اسپرسو با استفاده از SDK Manager و ساخت آن با استفاده از Gradle را پوشش می دهد. اندروید استودیو توصیه می شود.
محیط تست خود را تنظیم کنید
برای جلوگیری از پوسته پوسته شدن، ما به شدت توصیه می کنیم که انیمیشن های سیستم را در دستگاه های مجازی یا فیزیکی مورد استفاده برای آزمایش خاموش کنید. در دستگاه خود، در بخش تنظیمات > گزینههای برنامهنویس ، 3 تنظیمات زیر را غیرفعال کنید:
- مقیاس انیمیشن پنجره
- مقیاس انیمیشن انتقال
- مقیاس مدت زمان انیماتور
وابستگی های اسپرسو را اضافه کنید
برای افزودن وابستگی اسپرسو به پروژه خود، مراحل زیر را انجام دهید:
- فایل
build.gradle
برنامه خود را باز کنید. این معمولاً فایل build.gradle
سطح بالا نیست بلکه app/build.gradle
است. - خطوط زیر را درون وابستگی ها اضافه کنید:
شیار
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
androidTestImplementation 'androidx.test:runner:1.6.1'
androidTestImplementation 'androidx.test:rules:1.6.1'
کاتلین
androidTestImplementation('androidx.test.espresso:espresso-core:3.6.1')
androidTestImplementation('androidx.test:runner:1.6.1')
androidTestImplementation('androidx.test:rules:1.6.1')
مجموعه کامل وابستگی های Gradle را مشاهده کنید .
دونده ابزار دقیق را تنظیم کنید
خط زیر را در android.defaultConfig
به همان فایل build.gradle
اضافه کنید:
شیار
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
کاتلین
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
نمونه فایل ساخت Gradle
شیار
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'
}
کاتلین
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')
}
تجزیه و تحلیل
برای اینکه مطمئن شویم با هر نسخه جدید در مسیر درستی هستیم، دونده آزمایشی تجزیه و تحلیل ها را جمع آوری می کند. به طور خاص، یک هش از نام بسته برنامه تحت آزمایش را برای هر فراخوان آپلود می کند. این به ما امکان میدهد هم تعداد بستههای منحصربهفرد با استفاده از اسپرسو و هم حجم استفاده را اندازهگیری کنیم.
اگر نمیخواهید این دادهها را آپلود کنید، میتوانید با گنجاندن آرگومان disableAnalytics
در دستور ابزار دقیق خود انصراف دهید:
adb shell am instrument -e disableAnalytics true
تست اول را اضافه کنید
Android Studio به طور پیشفرض آزمایشها را در src/androidTest/java/com.example.package/
ایجاد میکند.
نمونه تست JUnit4 با استفاده از قوانین:
کاتلین
@RunWith(AndroidJUnit4::class)
@LargeTest
class HelloWorldEspressoTest {
@get:Rule
val activityRule = ActivityScenarioRule(MainActivity::class.java)
@Test fun listGoesOverTheFold() {
onView(withText("Hello world!")).check(matches(isDisplayed()))
}
}
جاوا
@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، مراحل زیر را انجام دهید:
- Run > Edit Configurations را باز کنید.
- یک پیکربندی Android Tests جدید اضافه کنید.
- یک ماژول را انتخاب کنید.
- یک رانر ابزار دقیق اضافه کنید:
androidx.test.runner.AndroidJUnitRunner
- پیکربندی جدید ایجاد شده را اجرا کنید.
از خط فرمان
دستور Gradle زیر را اجرا کنید:
./gradlew connectedAndroidTest
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و 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```"]]