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.
Hướng dẫn này mô tả cách chuẩn bị XCTest để kiểm thử trong Firebase Test Lab.
Bước 1: Định cấu hình vị trí Dữ liệu phái sinh của dự án
Xcode đặt các cấu phần phần mềm iOS đã biên dịch (bao gồm cả mọi kiểm thử mà bạn tạo) trong thư mục DerivedData. Bạn có thể giữ vị trí mặc định cho thư mục đó nếu muốn, nhưng thường thì bạn nên chọn một vị trí dễ truy cập hơn cho các tệp, đặc biệt là nếu bạn sẽ thường xuyên chạy các thử nghiệm bằng Test Lab:
Mở dự án của bạn trong Xcode.
Trong thanh trình đơn macOS, hãy chọn File (Tệp) > Project Settings... (Cài đặt dự án...)
Thay đổi trình đơn thả xuống Dữ liệu phái sinh từ Vị trí mặc định thành Vị trí tuỳ chỉnh.
Trong trường bên dưới trình đơn thả xuống, hãy chọn một vị trí để Xcode xuất các kiểm thử của bạn. (Đây là FOLDER_WITH_TEST_OUTPUT của bạn)
Bước 2: Tạo một tệp kiểm thử chung
Test Lab chạy các bài kiểm thử đơn vị và kiểm thử giao diện người dùng bằng khung XCTest.
Để chạy XCTest của ứng dụng trên các thiết bị Test Lab, hãy tạo bản dựng để kiểm thử trên một Thiết bị iOS chung:
Trong trình đơn thả xuống thiết bị ở đầu cửa sổ không gian làm việc Xcode, hãy chọn Generic iOS Device (Thiết bị iOS chung).
Trong thanh trình đơn macOS, hãy chọn Product (Sản phẩm) > Build For (Xây dựng cho) > Testing (Thử nghiệm).
Ngoài ra, bạn có thể tạo XCTest qua dòng lệnh.
Sử dụng lệnh sau trong một cửa sổ dòng lệnh:
Đảm bảo rằng tất cả các cấu phần phần mềm trong ứng dụng và kiểm thử đều được ký. Ví dụ: bạn có thể thực hiện việc này thông qua Xcode bằng cách chỉ định các chế độ cài đặt ký như hồ sơ cung cấp và danh tính. Để biết thêm thông tin, hãy xem bài viết Ký mã của Apple.
Xác minh chữ ký ứng dụng bằng cách chạy codesign --verify --deep --verbose /path/to/MyApp.app, trong đó "MyApp" là tên của ứng dụng bên trong thư mục đã giải nén. Điều này thay đổi tuỳ theo từng dự án.
Kết quả đầu ra dự kiến là MyApp.app: valid on disk.
Nếu đang chạy XCUITest, bạn cần xác minh quy trình kiểm thử và trình chạy bằng cách chạy codesign --verify --deep --verbose /path/to/MyTest-Runner.app, trong đó "MyTest" là tên của trình chạy bên trong thư mục đã giải nén. Mỗi dự án sẽ có một mức phí khác nhau. Kết quả đầu ra dự kiến là MyTest-Runner.app: valid on disk.
Bước 4: Đóng gói ứng dụng và kiểm thử để tải lên
Sau khi kiểm thử thành công, hãy nén kiểm thử để tải lên Test Lab:
cd FOLDER_WITH_TEST_OUTPUT/Build/Products : \
zip -r MyTests.zip Debug-iphoneos YOUR_SCHEME_iphoneosDEPLOYMENT_TARGET-arm64.xctestrun
Bạn cũng có thể đóng gói bài kiểm thử bằng cách nén các tệp kiểm thử theo cách thủ công:
Mở Finder rồi chuyển đến FOLDER_WITH_TEST_OUTPUT.
Mở thư mục có tên dự án của bạn làm tiền tố, sau đó chuyển đến thư mục Build/Products bên trong.
Chọn các thư mục Debug-iphoneos và YOUR_SCHEME_iphoneosDEPLOYMENT_TARGET-arm64.xctestrun rồi nén chúng.
Bước 5: (Không bắt buộc) Chạy thử nghiệm cục bộ
Trước khi chạy thử nghiệm bằng Test Lab, bạn có thể chạy thử nghiệm cục bộ bằng một thiết bị kết nối qua USB để kiểm tra chất lượng hành vi của thử nghiệm:
[null,null,["Cập nhật lần gần đây nhất: 2025-08-16 UTC."],[],[],null,["# Run an XCTest\n\n\u003cbr /\u003e\n\nThis guide describes how to prepare an XCTest for testing in\nFirebase Test Lab.\n\n**Step 1**: Configure your project's Derived Data location\n----------------------------------------------------------\n\nXcode places compiled iOS artifacts, including any tests you build, in a Derived\nData directory. It is possible to keep the default location for that directory,\nif you'd like, but it's often helpful to choose a more easily-accessible place\nfor the files, especially if you're going to be running tests with Test Lab\noften:\n\n1. Open your project in Xcode.\n2. In the macOS menu bar, select **File** \\\u003e **Project Settings...**\n3. Change the **Derived Data** drop-down from **Default Location** to **Custom Location**.\n4. In the field below the drop-down, select a location for Xcode to output your tests to. (This is your \u003cvar class=\"edit\" scope=\"FOLDER_WITH_TEST_OUTPUT\" translate=\"no\"\u003eFOLDER_WITH_TEST_OUTPUT\u003c/var\u003e)\n\n**Step 2**: Build a generic test file\n-------------------------------------\n\nTest Lab runs unit tests and UI tests using the\n[XCTest](https://developer.apple.com/documentation/xctest) framework.\nTo run your app's XCTests on Test Lab devices, build it for testing on a\nGeneric iOS Device:\n\n1. From the device drop-down at the top of your Xcode workspace window, select **Generic iOS Device**.\n2. In the macOS menu bar, select **Product** \\\u003e **Build For** \\\u003e **Testing**.\n\nAs an alternative, you can build your XCTest from the command line.\nUse the following command in a terminal: \n\n#### project\n\n```\nxcodebuild -project PATH/TO/YOUR_WORKSPACE/YOUR_PROJECT.xcodeproj \\\n -scheme YOUR_SCHEME \\\n -derivedDataPath FOLDER_WITH_TEST_OUTPUT \\\n -sdk iphoneos build-for-testing\n```\n\n#### workspace\n\n```\nxcodebuild -workspace PATH/TO/YOUR_WORKSPACE.xcworkspace \\\n -scheme YOUR_SCHEME \\\n -derivedDataPath FOLDER_WITH_TEST_OUTPUT \\\n -sdk iphoneos build-for-testing\n```\n\n**Step 3**: Sign your test and verify\n-------------------------------------\n\n1. Make sure all artifacts in the app and test are signed. For example, you can\n do this through Xcode by specifying signing settings like provisioning profile\n and identity. For more information, see\n [Apple Code Signing](https://developer.apple.com/support/code-signing/).\n\n | **Note** : Test Lab re-signs your app with its own provisioning profile and certificate.\n2. Verify app signature by running\n `codesign --verify --deep --verbose /path/to/MyApp.app` where \"MyApp\" is the\n name of the app inside the unzipped folder. This varies for each project.\n Expected output is `MyApp.app: valid on disk`.\n\n3. If you are running an XCUITest, then you need to verify the test and runner\n by running `codesign --verify --deep --verbose /path/to/MyTest-Runner.app` where\n \"MyTest\" is the name of the runner inside the unzipped folder. This varies for\n each project. Expected output is `MyTest-Runner.app: valid on disk`.\n\n**Step 4**: Package your app and test for uploading\n---------------------------------------------------\n\n1. After your test successfully builds, zip it for upload to Test Lab:\n\n ```\n cd FOLDER_WITH_TEST_OUTPUT/Build/Products : \\\n zip -r MyTests.zip Debug-iphoneos YOUR_SCHEME_iphoneosDEPLOYMENT_TARGET-arm64.xctestrun\n ```\n\nYou can also package up your test by compressing the test files manually:\n\n1. Open Finder and navigate to\n \u003cvar class=\"edit\" scope=\"FOLDER_WITH_TEST_OUTPUT\" translate=\"no\"\u003eFOLDER_WITH_TEST_OUTPUT\u003c/var\u003e.\n\n2. Open the folder that has your project name as a prefix, then navigate to\n `Build/Products` folder inside.\n\n3. Select the folders `Debug-iphoneos` and\n \u003cvar class=\"edit\" scope=\"YOUR_SCHEME\" translate=\"no\"\u003eYOUR_SCHEME\u003c/var\u003e`_iphoneos`\u003cvar class=\"edit\" scope=\"DEPLOYMENT_TARGET\" translate=\"no\"\u003eDEPLOYMENT_TARGET\u003c/var\u003e`-arm64.xctestrun`\n and then compress them.\n\n**Step 5**: (Optional) Run your test locally\n--------------------------------------------\n\nBefore running your test with Test Lab, you can run it locally with\na USB-connected device to quality check its behavior: \n\n```\nxcodebuild test-without-building \\\n -xctestrun \"Derived Data/Build/Products/YOUR_SCHEME.xctestrun\" \\\n -destination id=your-phone-id\n```\n\nNext steps\n----------\n\nUpload and run your test in the [Firebase console](/docs/test-lab/ios/firebase-console)\nor the [gcloud CLI](/docs/test-lab/ios/command-line)."]]