當遊戲應用程式建立在不同的 UI 框架上時,自動化遊戲測試可能會很困難。遊戲循環測試可讓您將本機測試與測試實驗室集成,並輕鬆在您選擇的裝置上運行它們。本指南介紹如何準備要使用 Firebase 測試實驗室運行的遊戲循環測試。
關於遊戲循環測試
什麼是遊戲循環測試?
遊戲循環測試模擬真實玩家的操作,以快速且可擴展的方式驗證您的遊戲是否為使用者運作良好。循環是對遊戲應用程式進行完整或部分測試。您可以在模擬器上或測試實驗室中的一組裝置上本地執行遊戲循環測試。遊戲循環測試可用於:
- 以最終用戶玩遊戲的方式運行您的遊戲。你可以將使用者的輸入編寫成腳本,讓使用者閒置,或用AI取代使用者(例如,如果你在賽車遊戲中實現了AI,你可以讓一個AI驅動程式負責使用者的輸入) 。
- 以最高品質設定運行遊戲,看看哪些設備可以支援它。
- 執行技術測試,例如編譯多個著色器、執行它們並檢查輸出是否符合預期。
第 1 步:註冊測試實驗室的自訂 URL 方案
在 Xcode 中,選擇一個專案目標。
按一下「資訊」選項卡,然後新增新的URL 類型。
在URL 方案欄位中,輸入
firebase-game-loop
。您也可以透過將自訂 URL 方案新增至專案的Info.plist
設定檔<dict>
標記內的任意位置來註冊自訂 URL 方案:<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLName</key> <string></string> <key>CFBundleTypeRole</key> <string>Editor</string> <key>CFBundleURLSchemes</key> <array> <string>firebase-game-loop</string> </array> </dict> </array>
您的應用程式現已配置為使用測試實驗室執行測試。
第 2 步:(可選)配置您的應用程式
運行多個循環
如果您打算在測試中執行多個循環(也稱為場景),則必須指定啟動時要在應用程式中執行哪些循環。
在您的應用程式委託中,重寫application(_:open:options:)
方法:
迅速
func application(_app: UIApplication,
open url: URL
options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
let components = URLComponents(url: url, resolvingAgainstBaseURL: true)!
if components.scheme == "firebase-game-loop" {
// ...Enter Game Loop Test logic to override application(_:open:options:).
}
return true
}
Objective-C
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary <UIApplicationOpenURLOptionsKey, id> *)options {
if ([url.scheme isEqualToString:(@"firebase-game-loop")]) {
// ...Enter Game Loop Test logic to override application(_:open:options:).
}
}
當您在測試中執行多個循環時,當前循環將作為參數傳遞給用於啟動應用程式的 URL。您也可以透過解析用於取得自訂 URL 方案的URLComponents
物件來取得目前循環編號:
迅速
if components.scheme == "firebase-game-loop" {
// Iterate over all parameters and find the one with the key "scenario".
let scenarioNum = Int(components.queryItems!.first(where: { $0.name == "scenario" })!.value!)!
// ...Write logic specific to the current loop (scenarioNum).
}
Objective-C
if ([url.scheme isEqualToString:(@"firebase-game-loop")]) {
// Launch the app as part of a game loop.
NSURLComponents *components = [NSURLComponents componentsWithURL:url
resolvingAgainstBaseURL:YES];
for (NSURLQueryItem *item in [components queryItems]) {
if ([item.name isEqualToString:@"scenario"]) {
NSInteger scenarioNum = [item.value integerValue];
// ...Write logic specific to the current loop (scenarioNum).
}
}
}
提前結束測試
預設情況下,即使所有循環都已執行,遊戲循環測試也會繼續運行,直到達到五分鐘超時。當達到超時時,測試結束並取消任何掛起的循環。您可以透過在應用程式的 AppDelegate 中呼叫測試實驗室的自訂 URL 方案firebase-game-loop-complete
來加快測試速度或提前結束測試。例如:
迅速
/// End the loop by calling our custom url scheme.
func finishLoop() {
let url = URL(string: "firebase-game-loop-complete://")!
UIApplication.shared.open(url)
}
Objective-C
- (void)finishLoop {
UIApplication *app = [UIApplication sharedApplication];
[app openURL:[NSURL URLWithString:@"firebase-game-loop-complete://"]
options:@{}
completionHandler:^(BOOL success) {}];
}
您的遊戲循環測試將終止當前循環並執行下一個循環。當不再有循環可運行時,測試結束。
編寫自訂測試結果
您可以設定遊戲循環測試以將自訂測試結果寫入裝置的檔案系統。這樣,當測試開始運行時,測試實驗室會將結果檔案儲存在測試裝置上的GameLoopsResults
目錄中(您必須自行建立該目錄)。測試結束後,測試實驗室會將所有檔案從GameLoopResults
目錄移至專案的儲存桶中。設定測試時請記住以下幾點:
無論文件類型、大小或數量如何,所有結果文件都會上傳。
在測試中的所有循環完成運行之前,測試實驗室不會處理您的測試結果,因此,如果您的測試包含多個寫入輸出的循環,請確保將它們附加到唯一的結果檔案或為每個循環建立一個結果檔。這樣,您可以避免覆蓋先前循環的結果。
要設定測試以寫入自訂測試結果:
在應用程式的
Documents
目錄中,建立一個名為GameLoopResults
的目錄。從應用程式程式碼中的任意位置(例如,您的應用程式委託)新增以下內容:
迅速
/// Write to a results file. func writeResults() { let text = "Greetings from game loops!" let fileName = "results.txt" let fileManager = FileManager.default do { let docs = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) let resultsDir = docs.appendingPathComponent("GameLoopResults") try fileManager.createDirectory( at: resultsDir, withIntermediateDirectories: true, attributes: nil) let fileURL = resultsDir.appendingPathComponent(fileName) try text.write(to: fileURL, atomically: false, encoding: .utf8) } catch { // ...Handle error writing to file. } }
Objective-C
/// Write to a results file. - (void)writeResults:(NSString *)message { // Locate and create the results directory (if it doesn't exist already). NSFileManager *manager = [NSFileManager defaultManager]; NSURL* url = [[manager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; NSURL* resultsDir = [url URLByAppendingPathComponent:@"GameLoopResults" isDirectory:YES]; [manager createDirectoryAtURL:resultsDir withIntermediateDirectories:NO attributes:nil error:nil]; // Write the result message to a text file. NSURL* resultFile = [resultsDir URLByAppendingPathComponent:@"result.txt"]; if ([manager fileExistsAtPath:[resultFile path]]) { // Append to the existing file NSFileHandle *handle = [NSFileHandle fileHandleForWritingToURL:resultFile error:nil]; [handle seekToEndOfFile]; [handle writeData:[message dataUsingEncoding:NSUTF8StringEncoding]]; [handle closeFile]; } else { // Create and write to the file. [message writeToURL:resultFile atomically:NO encoding:NSUTF8StringEncoding error:nil]; } }
第 3 步:簽署您的應用程式
確保應用程式中的所有工件都已簽署。例如,您可以透過 Xcode 指定設定檔和身分等簽章設定來執行此操作。有關更多信息,請參閱: Apple 協同設計
第 4 步:打包您的應用程式以供上傳
為您的應用程式產生 IPA 檔案(您稍後需要找到它)。
從出現的下拉式選單中,按一下產品 > 存檔。選擇最新的存檔,然後按一下「分發應用程式」 。
在出現的視窗中,按一下「開發」>「下一步」 。
可選:若要獲得更快的建置速度,請取消選擇「從位碼重建」選項,然後按一下「下一步」 。測試實驗室不需要精簡或重建您的應用程式來執行測試,因此您可以安全地停用此選項。
按一下匯出,然後輸入您要下載應用程式的 IPA 檔案的目錄。
第 5 步:驗證應用程式簽名
- 透過解壓縮 .ipa 文件,然後執行
codesign --verify --deep --verbose /path/to/MyApp.app
來驗證應用程式簽名,其中「MyApp」是解壓縮資料夾中應用程式的名稱(因每個項目而異) )。預期輸出為MyApp.app: valid on disk
。
第 6 步:在本地運行測試
您可以在本機上執行測試以檢查其行為,然後再使用測試實驗室執行測試。要進行本機測試,請在模擬器中載入您的遊戲應用程式並執行:
xcrun simctl openurl SIMULATOR_UDID firebase-game-loop://
您可以透過執行
instruments -s devices
指令找到模擬器的UDID。如果只執行一個模擬器,請輸入特殊字串
"booted"
來取代SIMULATOR_UDID 。
如果您的測試包含多個循環,您可以透過將循環編號傳遞給scenario
標誌來指定要執行的循環。請注意,在本地運行測試時一次只能運行一個循環。例如,如果要執行循環 1、2 和 5,則必須為每個循環執行單獨的命令:
xcrun simctl openurl SIMULATOR_UDID firebase-game-loop://?scenario=1
xcrun simctl openurl SIMULATOR_UDID firebase-game-loop://?scenario=2
xcrun simctl openurl SIMULATOR_UDID firebase-game-loop://?scenario=5
下一步
使用Firebase 控制台或gcloud CLI執行測試。