اختبار تنسيق Compose
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
اختبِر واجهة مستخدم تطبيقك للتأكّد من أنّ سلوك رمز Compose صحيح. يتيح لك ذلك رصد الأخطاء مبكرًا وتحسين جودة تطبيقك.
توفّر Compose مجموعة من واجهات برمجة التطبيقات للاختبار من أجل العثور على العناصر والتحقّق من سماتها وتنفيذ إجراءات المستخدم. وتتضمّن واجهات برمجة التطبيقات أيضًا ميزات متقدّمة، مثل التلاعب بالوقت. استخدِم واجهات برمجة التطبيقات هذه لإنشاء اختبارات قوية تتحقّق من سلوك تطبيقك.
المشاهدات
إذا كنت تعمل باستخدام طرق العرض بدلاً من Compose، اطّلِع على القسم العام اختبار التطبيقات على Android.
على وجه الخصوص، يُعدّ دليل أتمتة اختبارات واجهة المستخدم مكانًا جيدًا للبدء. توضّح هذه الصفحة كيفية إعداد اختبارات تلقائية يتم تنفيذها على الجهاز، بما في ذلك عند استخدام طرق العرض.
المفاهيم الرئيسية
في ما يلي بعض المفاهيم الأساسية لاختبار رمز Compose البرمجي:
- الدلالات: تمنح الدلالات معنى لواجهة المستخدم، ما يتيح للاختبارات التفاعل مع عناصر معيّنة.
- واجهات برمجة التطبيقات الخاصة بالاختبار: تتيح لك واجهات برمجة التطبيقات الخاصة بالاختبار العثور على العناصر والتحقّق من سماتها وتنفيذ إجراءات المستخدم.
- المزامنة: تتحقّق المزامنة من أنّ الاختبارات تنتظر أن تصبح واجهة المستخدم غير نشطة قبل تنفيذ الإجراءات أو تقديم التأكيدات.
- إمكانية التشغيل التفاعلي: تتيح إمكانية التشغيل التفاعلي إجراء الاختبارات على عناصر مستندة إلى كل من Compose وView في التطبيق نفسه.
ورقة الغش الخاصة بالاختبار
اطّلِع على ورقة الغش الخاصة بالاختبار للحصول على نظرة عامة على جميع المواضيع الرئيسية التي يجب التعرّف عليها بشأن الاختبار في Compose.
ضبط إعدادات الميزة
إعداد تطبيقك للسماح لك باختبار رمز الإنشاء
أولاً، أضِف التبعيات التالية إلى ملف build.gradle
الخاص بالوحدة التي تحتوي على اختبارات واجهة المستخدم:
// Test rules and transitive dependencies:
androidTestImplementation("androidx.compose.ui:ui-test-junit4:$compose_version")
// Needed for createComposeRule(), but not for createAndroidComposeRule<YourActivity>():
debugImplementation("androidx.compose.ui:ui-test-manifest:$compose_version")
تتضمّن هذه الوحدة ComposeTestRule
وتنفيذًا لنظام التشغيل Android
يُسمى AndroidComposeTestRule
. من خلال هذه القاعدة، يمكنك ضبط محتوى Compose أو الوصول إلى النشاط. يمكنك إنشاء القواعد باستخدام دوال المصنع،
إما createComposeRule
أو createAndroidComposeRule
إذا كنت بحاجة إلى الوصول إلى نشاط. يبدو اختبار واجهة المستخدم النموذجي في Compose على النحو التالي:
// file: app/src/androidTest/java/com/package/MyComposeTest.kt
class MyComposeTest {
@get:Rule val composeTestRule = createComposeRule()
// use createAndroidComposeRule<YourActivity>() if you need access to
// an activity
@Test
fun myTest() {
// Start the app
composeTestRule.setContent {
MyAppTheme {
MainScreen(uiState = fakeUiState, /*...*/)
}
}
composeTestRule.onNodeWithText("Continue").performClick()
composeTestRule.onNodeWithText("Welcome").assertIsDisplayed()
}
}
مراجع إضافية
- اختبار التطبيقات على Android: تقدّم صفحة Android الرئيسية الخاصة بالاختبار نظرة عامة على أساسيات الاختبار وأساليبه.
- أساسيات الاختبار: مزيد من المعلومات
عن المفاهيم الأساسية لاختبار تطبيق Android
- الاختبارات المحلية: يمكنك إجراء بعض الاختبارات محليًا على محطة العمل الخاصة بك.
- الاختبارات المبرمَجة: من الممارسات الجيدة أيضًا إجراء اختبارات مبرمَجة. أي الاختبارات التي يتم إجراؤها مباشرةً على الجهاز.
- التكامل المستمر:
يتيح لك التكامل المستمر دمج اختباراتك في مسار النشر.
- اختبار أحجام الشاشات المختلفة: بما أنّ المستخدمين يتوفّر لديهم العديد من الأجهزة، عليك اختبار أحجام الشاشات المختلفة.
- Espresso: على الرغم من أنّ Espresso مخصّصة لواجهات المستخدم المستندة إلى العرض، يمكن أن تكون معرفة Espresso مفيدة في بعض جوانب اختبار Compose.
Codelab
لمزيد من المعلومات، جرِّب التدريب العملي حول اختبار Jetpack Compose.
نماذج
اقتراحات مخصصة لك
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ 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,["Test your app's UI to verify that behavior of your Compose code is\ncorrect. This lets you catch errors early and improve the quality of your app.\n\nCompose provides a set of testing APIs to find elements, verify their\nattributes, and perform user actions. The APIs also include advanced features\nsuch as time manipulation. Use these APIs to create robust tests that verify\nyour app's behavior.\n| **Important:** For more on testing Android apps in general, including testing `View` elements, see the [general testing section](/training/testing). A good place to start is the [Fundamentals of testing Android apps](/training/testing/fundamentals) guide.\n\nViews\n\nIf you are working with views instead of Compose, see the general [Test apps on\nAndroid](/training/testing) section.\n\nIn particular, a good place to start is the [Automate UI tests](/training/testing/ui-tests) guide. It\nlays out how you can automate tests that run on-device, including when using\nviews.\n\nKey Concepts\n\nThe following are some key concepts for testing your Compose code:\n\n- **[Semantics](/develop/ui/compose/testing/semantics)**: Semantics give meaning to your UI, allowing tests to interact with specific elements.\n- **[Testing APIs](/develop/ui/compose/testing/apis)**: Testing APIs let you find elements, verify their attributes, and perform user actions.\n- **[Synchronization](/develop/ui/compose/testing/synchronization)**: Synchronization verifies that tests wait for the UI to be idle before performing actions or making assertions.\n- **[Interoperability](/develop/ui/compose/testing/interoperability)**: Interoperability enables tests to work with both Compose and View-based elements in the same app.\n\nTesting cheatsheet\n\nSee the [testing cheatsheet](/develop/ui/compose/testing/testing-cheatsheet) for an overview of all the key topics you should\nlearn about testing in Compose.\n\nSetup\n\nSet up your app to let you test compose code.\n\nFirst, add the following dependencies to the `build.gradle` file of the module\ncontaining your UI tests: \n\n // Test rules and transitive dependencies:\n androidTestImplementation(\"androidx.compose.ui:ui-test-junit4:$compose_version\")\n // Needed for createComposeRule(), but not for createAndroidComposeRule\u003cYourActivity\u003e():\n debugImplementation(\"androidx.compose.ui:ui-test-manifest:$compose_version\")\n\nThis module includes a [`ComposeTestRule`](/reference/kotlin/androidx/compose/ui/test/junit4/ComposeTestRule) and an implementation for Android\ncalled [`AndroidComposeTestRule`](/reference/kotlin/androidx/compose/ui/test/junit4/AndroidComposeTestRule). Through this rule you can set Compose\ncontent or access the activity. You construct the rules using factory functions,\neither [`createComposeRule`](/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createComposeRule()) or, if you need access to an activity,\n[`createAndroidComposeRule`](/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createAndroidComposeRule()). A typical UI test for Compose looks like this: \n\n // file: app/src/androidTest/java/com/package/MyComposeTest.kt\n\n class MyComposeTest {\n\n @get:Rule val composeTestRule = createComposeRule()\n // use createAndroidComposeRule\u003cYourActivity\u003e() if you need access to\n // an activity\n\n @Test\n fun myTest() {\n // Start the app\n composeTestRule.setContent {\n MyAppTheme {\n MainScreen(uiState = fakeUiState, /*...*/)\n }\n }\n\n composeTestRule.onNodeWithText(\"Continue\").performClick()\n\n composeTestRule.onNodeWithText(\"Welcome\").assertIsDisplayed()\n }\n }\n\nAdditional Resources\n\n- **[Test apps on Android](/training/testing)**: The main Android testing landing page provides a broader view of testing fundamentals and techniques.\n- **[Fundamentals of testing](/training/testing/fundamentals):** Learn more about the core concepts behind testing an Android app.\n- **[Local tests](/training/testing/local-tests):** You can run some tests locally, on your own workstation.\n- **[Instrumented tests](/training/testing/instrumented-tests):** It is good practice to also run instrumented tests. That is, tests that run directly on-device.\n- **[Continuous integration](/training/testing/continuous-integration):** Continuous integration lets you integrate your tests into your deployment pipeline.\n- **[Test different screen sizes](/training/testing/different-screens):** With some many devices available to users, you should test for different screen sizes.\n- **[Espresso](/training/testing/espresso)**: While intended for View-based UIs, Espresso knowledge can still be helpful for some aspects of Compose testing.\n\nCodelab\n\nTo learn more, try the [Jetpack Compose Testing codelab](/codelabs/jetpack-compose-testing).\n\nSamples\n\nRecommended for you\n\n- Note: link text is displayed when JavaScript is off\n- [Semantics in Compose](/develop/ui/compose/semantics)\n- [Window insets in Compose](/develop/ui/compose/layouts/insets)\n- [Other considerations](/develop/ui/compose/migrate/other-considerations)"]]