Kể từ ngày 27 tháng 3 năm 2025, bạn nên sử dụng android-latest-release
thay vì aosp-main
để xây dựng và đóng góp cho AOSP. Để biết thêm thông tin, hãy xem phần Thay đổi đối với AOSP.
Kiểm thử máy chủ JAR
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Bạn nên triển khai các kiểm thử máy chủ lưu trữ tệp lưu trữ Java (JAR) để cung cấp phạm vi mã hoàn chỉnh cho phần mềm của mình. Làm theo hướng dẫn để Tạo chương trình kiểm thử đơn vị cục bộ.
Viết các chương trình kiểm thử đơn vị nhỏ để xác thực một hàm cụ thể và không làm gì khác.
Ví dụ
Tệp Blueprint sau đây cung cấp một ví dụ đơn giản về kiểm thử máy chủ JAR Hello World để sao chép và điều chỉnh theo nhu cầu của bạn: platform_testing/tests/example/jarhosttest/Android.bp
Mã này tương ứng với mã kiểm thử thực tế có tại: platform_testing/tests/example/jarhosttest/test/android/test/example/helloworld/HelloWorldTest.java
Để thuận tiện, chúng tôi đưa ảnh chụp nhanh của tệp Blueprint vào đây:
java_test_host {
name: "HelloWorldHostTest",
test_suites: ["general-tests"],
srcs: ["test/**/*.java"],
static_libs: [
"junit",
"mockito",
],
}
Phần khai báo java_test_host
ở đầu cho biết đây là kiểm thử máy chủ JAR. Xem ví dụ về cách sử dụng trong:
frameworks/base/tools/powermodel/Android.bp
Cài đặt
Hãy xem phần giải thích bên dưới về các chế độ cài đặt sau:
Bạn phải đặt name
khi chỉ định loại mô-đun java_test_host
(ở đầu khối). Chế độ cài đặt này đặt tên cho mô-đun và tệp JAR thu được sẽ có cùng tên và hậu tố .jar
. Trong ví dụ dưới đây,tệp JAR kiểm thử thu được có tên là HelloWorldHostTest.jar
. Ngoài ra, chế độ cài đặt này cũng xác định tên mục tiêu tạo cho mô-đun của bạn, nhờ đó bạn có thể sử dụng make [options] <HelloWorldHostTest>
để tạo mô-đun kiểm thử và tất cả các phần phụ thuộc của mô-đun đó.
name: "HelloWorldHostTest",
Chế độ cài đặt test_suites
giúp bộ kiểm thử Trade Federation dễ dàng phát hiện được kiểm thử. Bạn có thể thêm các bộ kiểm thử khác vào đây, chẳng hạn như CTS, để có thể chia sẻ kiểm thử máy chủ JAR.
test_suites: ["general-tests"],
Chế độ cài đặt static_libs
hướng dẫn hệ thống xây dựng kết hợp nội dung của các mô-đun được đặt tên vào tệp APK thu được của mô-đun hiện tại.
Điều này có nghĩa là mỗi mô-đun được đặt tên dự kiến sẽ tạo một tệp .jar
.
Nội dung của mô-đun được dùng để phân giải các tệp tham chiếu đường dẫn lớp trong thời gian biên dịch và được đưa vào tệp APK thu được.
static_libs: [
"junit",
],
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],null,["# JAR host tests\n\nJava archive (JAR) host tests should be implemented to provide complete code\ncoverage of your software. Follow the instructions to [Build local unit\ntests](https://developer.android.com/training/testing/unit-testing/local-unit-tests).\nWrite small unit tests to validate a specific function and nothing more.\n\nExample\n-------\n\nThe following Blueprint file provides a simple Hello World JAR host test example\nto copy and adapt to your needs:\n[platform_testing/tests/example/jarhosttest/Android.bp](https://android.googlesource.com/platform/platform_testing/+/android16-release/tests/example/jarhosttest/Android.bp)\n\nThis corresponds to the actual test code found at:\n[platform_testing/tests/example/jarhosttest/test/android/test/example/helloworld/HelloWorldTest.java](https://android.googlesource.com/platform/platform_testing/+/android16-release/tests/example/jarhosttest/test/android/test/example/helloworld/HelloWorldTest.java)\n\nA snapshot of the Blueprint file is included here for convenience: \n\n java_test_host {\n name: \"HelloWorldHostTest\",\n\n test_suites: [\"general-tests\"],\n\n srcs: [\"test/**/*.java\"],\n\n static_libs: [\n \"junit\",\n \"mockito\",\n ],\n }\n\nThe `java_test_host` declaration at the beginning indicates that this is a JAR\nhost test. See an example of its use in:\n[frameworks/base/tools/powermodel/Android.bp](https://android.googlesource.com/platform/frameworks/base/+/refs/heads/android16-release/tools/powermodel/Android.bp)\n\nSettings\n--------\n\nSee below for explanations of the following settings:\n\n- The `name` setting is required when the `java_test_host` module type is\n specified (at the start of the block). This setting gives a name to your\n module, and the resulting JAR has the same name and a `.jar` suffix. In the\n example below,the resulting test JAR is named `HelloWorldHostTest.jar`. In\n addition, this setting also defines a make target name for your module, so\n that you can use `make [options] \u003cHelloWorldHostTest\u003e` to build your test\n module and all its dependencies.\n\n name: \"HelloWorldHostTest\",\n\n- The `test_suites` setting makes the test easily discoverable by the Trade\n Federation test harness. Other test suites can be added here, such as CTS,\n so that the JAR host test test can be shared.\n\n test_suites: [\"general-tests\"],\n\n- The `static_libs` setting instructs the build system to incorporate the\n contents of the named modules into the resulting APK of the current module.\n This means that each named module is expected to produce a `.jar` file.\n The module's content is used for resolving classpath references during\n compile time and incorporated into the resulting APK.\n\n static_libs: [\n \"junit\",\n ],"]]