اعتبارًا من 27 آذار (مارس) 2025، ننصحك باستخدام android-latest-release بدلاً من aosp-main لإنشاء AOSP والمساهمة فيه. لمزيد من المعلومات، يُرجى الاطّلاع على التغييرات في AOSP.
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يُرجى أولاً قراءة مقالة اختبار تطبيقك
على developer.android.com. يُرجى العِلم أنّ هناك بعض الاختلافات في
كيفية استخدام اختبارات الأدوات في اختبار المنصة.
باختصار، يقدّم اختبار الأدوات بيئة تنفيذ اختبار خاصة يتم تشغيلها من خلال الأمر am instrument، حيث تتم إعادة تشغيل عملية التطبيق المستهدفة وإعدادها باستخدام سياق التطبيق الأساسي، ويتم بدء سلسلة مهام أدوات القياس داخل الجهاز الافتراضي لعملية التطبيق. يبدأ
رمز الاختبار التنفيذ في سلسلة الأدوات هذه ويتم تزويده
بمثيل Instrumentation يتيح الوصول إلى سياق التطبيق
وواجهات برمجة التطبيقات للتلاعب بعملية التطبيق التي يتم اختبارها.
المفاهيم الرئيسية
يجب الإعلان عن أداة قياس في حزمة تطبيق باستخدام علامة
<instrumentation>
مدمجة ضمن علامة <manifest> في بيان حزمة التطبيق.
قد يحتوي بيان حزمة التطبيق من الناحية الفنية على عدّة علامات
<instrumentation>، على الرغم من أنّه لا يتم استخدامه بشكل شائع بهذه الطريقة.
يجب أن يحتوي كل <instrumentation> على ما يلي:
سمة android:name: يجب أن تكون اسمًا لفئة فرعية من
Instrumentation
المضمّنة في تطبيق الاختبار، والذي يكون عادةً أداة
تشغيل الاختبار المستخدَمة، على سبيل المثال:
android.support.test.runner.AndroidJUnitRunner
يجب تحديد سمة android:targetPackage. يجب ضبط قيمته
على حزمة التطبيق التي يتم اختبارها.
ملخّص الخطوات
في ما يلي الوجهات الشائعة للاختبارات الصارمة ضد خدمات إطار العمل:
اتّباع الاصطلاح الحالي في حال إضافة اختبارات إلى أحد
المواقع أعلاه إذا كنت بصدد إعداد وحدة اختبار جديدة، يُرجى اتّباع خطوات AndroidManifest.xml وAndroid.mk في أحد المواقع الجغرافية المذكورة أعلاه.
adb shell am instrument -w -e class \
android.animation.AnimatorSetEventsTest \
com.android.frameworks.coretests\
/android.support.test.runner.AndroidJUnitRunner
طريقة اختبار معيّنة
adb shell am instrument -w -e class \
android.animation.AnimatorSetEventsTest#testCancel \
com.android.frameworks.coretests\
/android.support.test.runner.AndroidJUnitRunner
يمكن أن يُجري اختبارك تأكيدًا صريحًا على النجاح أو التوقّف باستخدام واجهات برمجة التطبيقات JUnit. بالإضافة إلى ذلك، سيؤدي أي استثناءات لم يتمّ رصدها إلى حدوث تعذُّر وظيفي أيضًا.
لعرض مقاييس الأداء، يمكن لرمز الاختبار استدعاء
Instrumentation#sendStatus
لإرسال قائمة بأزواج المفتاح/القيمة. من المهم ملاحظة ما يلي:
يمكن أن تكون المقاييس عددًا صحيحًا أو عددًا عشريًا.
سيتم تجاهل أي قيم غير رقمية.
يمكن أن يكون حِزمة APK الاختبارية إما اختبارات وظيفية أو اختبارات مقاييس، ولكن
لا يمكن حاليًا دمج كليهما.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-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-07-27 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["# Instrumentation tests\n\nFirst read [Test your app](https://developer.android.com/studio/test/)\non developer.android.com. Take note there are some differences in\nhow instrumentation tests are used in platform testing.\n\nIn summary, an instrumentation test provides a special test execution\nenvironment as launched via the `am instrument` command, where the targeted\napplication process is restarted and initialized with basic application context,\nand an instrumentation thread is started inside the application process VM. Your\ntest code starts execution on this instrumentation thread and is provided with\nan `Instrumentation` instance that provides access to the application context\nand APIs to manipulate the application process under test.\n\nKey concepts\n------------\n\n- an instrumentation must be declared in an application package, with an [`\u003cinstrumentation\u003e`](https://developer.android.com/guide/topics/manifest/instrumentation-element.html) tag nested under the `\u003cmanifest\u003e` tag of the application package manifest.\n- an application package manifest may technically contain multiple `\u003cinstrumentation\u003e` tags, though it's not commonly used in this fashion.\n- each `\u003cinstrumentation\u003e` must contain:\n - an `android:name` attribute: it should be the name of a subclass of [`Instrumentation`](https://developer.android.com/reference/android/app/Instrumentation.html) that's included in the test application, which is typically the test runner that's being used, e.g.: `android.support.test.runner.AndroidJUnitRunner`\n - an `android:targetPackage` attribute must be defined. Its value should be set to the application package under test.\n\nSummary of steps\n----------------\n\n1. Below are common destinations for hermetic tests against framework services:\n\n frameworks/base/core/tests/coretests\n frameworks/base/services/tests/servicestests\n\n If you are adding a brand new instrumentation module for your component, see\n - [Self-Instrumenting Tests: A Complete Example](/docs/core/tests/development/instr-self-e2e)\n - [Instrumentation Targeting an Application: A Complete Example](/docs/core/tests/development/instr-app-e2e)\n2. Following the existing convention if you are adding tests into one of the\n locations above. If you are setting up a new test module, please follow the\n setup of `AndroidManifest.xml` and `Android.mk` in one of the locations\n above\n\n3. See\n [frameworks/base/core/tests/coretests/](https://android.googlesource.com/platform/frameworks/base.git/+/android16-release/core/tests/coretests/)\n for an example.\n Note these lines install extra apps:\n\n \u003coption name=\"test-file-name\" value=\"FrameworksCoreTests.apk\" /\u003e\n \u003coption name=\"test-file-name\" value=\"BstatsTestApp.apk\" /\u003e\n\n4. Do not forget to mark your test as `@SmallTest`, `@MediumTest` or\n `@LargeTest`\n\n5. Build the test module with m, e.g.:\n\n m FrameworksCoreTests\n\n6. Run the tests:\n\n - The simplest solution is to use\n [Atest](/docs/core/tests/development/atest) like so:\n\n atest FrameworksCoreTests\n\n - Or for more complex tests, use the\n [Trade Federation test Harness](/docs/core/tests/tradefed):\n\n m tradefed-all\n tradefed.sh run template/local_min --template:map test=FrameworksCoreTests\n\n7. If not using Tradefed, manually install and run the tests:\n\n 1. Install the generated apk:\n\n adb install -r ${OUT}/data/app/FrameworksCoreTests/FrameworksCoreTests.apk\n\n | **Tip:** you use `adb shell pm list instrumentation` to find the instrumentations inside the apk just installed\n 1. Run the tests with various options:\n\n 1. all tests in the apk\n\n adb shell am instrument -w com.android.frameworks.coretests\\\n /android.support.test.runner.AndroidJUnitRunner\n\n 2. all tests under a specific Java package\n\n adb shell am instrument -w -e package android.animation \\\n com.android.frameworks.coretests\\\n /android.support.test.runner.AndroidJUnitRunner\n\n 3. all tests under a specific class\n\n adb shell am instrument -w -e class \\\n android.animation.AnimatorSetEventsTest \\\n com.android.frameworks.coretests\\\n /android.support.test.runner.AndroidJUnitRunner\n\n 4. a specific test method\n\n adb shell am instrument -w -e class \\\n android.animation.AnimatorSetEventsTest#testCancel \\\n com.android.frameworks.coretests\\\n /android.support.test.runner.AndroidJUnitRunner\n\nYour test can make an explicit assertion on pass or fail using `JUnit` APIs; in\naddition, any uncaught exceptions will also cause a functional failure.\n\nTo emit performance metrics, your test code can call\n[`Instrumentation#sendStatus`](http://developer.android.com/reference/android/app/Instrumentation.html#sendStatus(int,%20android.os.Bundle))\nto send out a list of key-value pairs. It's important to note that:\n\n1. metrics can be integer or floating point\n2. any non-numerical values will be discarded\n3. your test apk can be either functional tests or metrics tests, however mixing both are not currently supported"]]