Firebase is back at Google I/O on May 10! Register now

開始使用 iOS 的 Game Loop 測試

透過集合功能整理內容 你可以依據偏好儲存及分類內容。

通過 Game Loop 測試,您可以為您的遊戲引擎編寫本機測試,然後在您選擇的設備上的測試實驗室中運行它們。這樣,您就不必擔心為不同的 UI 或測試框架編寫代碼。遊戲循環測試模擬真實玩家的動作,當您在測試實驗室中運行它時,它提供了一種快速且可擴展的方法來驗證您的遊戲是否為您的用戶提供良好的性能。

此頁面向您展示如何運行遊戲循環測試,然後在 Firebase 控制台的測試實驗室頁面中查看和管理您的測試結果。您還可以使用可選功能進一步自定義測試,例如編寫自定義測試結果提前結束測試

什麼是遊戲循環測試?

循環是您對遊戲應用程序進行的完整或部分測試。您可以在模擬器或測試實驗室中的一組設備上本地運行遊戲循環測試。遊戲循環測試可用於:

  • 以最終用戶玩遊戲的方式運行您的遊戲。你可以編寫用戶輸入的腳本,讓用戶空閒,或者用 AI 代替用戶(例如,如果你在賽車遊戲中實現了 AI,你可以讓 AI 司機負責用戶的輸入) .

  • 以最高質量設置運行您的遊戲,以了解哪些設備可以支持它。

  • 運行技術測試,例如編譯多個著色器、執行它們並檢查輸出是否符合預期。

第 1 步:註冊測試實驗室的自定義 URL 架構

首先,您必須在您的應用中註冊 Firebase 測試實驗室的自定義 URL 方案:

  1. 在 Xcode 中,選擇一個項目目標。

  2. 單擊信息選項卡,然後添加新的URL 類型

  3. URL Schemes字段中,輸入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 步(可選):將您的應用配置為運行多個循環

如果您的應用程序註冊了多個自定義 URL 方案並且您計劃在測試中運行多個循環(也稱為場景),則必須在啟動時指定要在應用程序中運行的循環。

在您的應用委託中,重寫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
}

目標-C

- (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
            options:(NSDictionary &lt;UIApplicationOpenURLOptionsKey, id&gt; *)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).
}

目標-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).
        }
    }
}

第 3 步:創建並運行測試

註冊測試實驗室的自定義 URL 架構後,您可以在Firebase 控制台或使用gcloud beta CLI運行測試。如果您還沒有為您的應用程序生成一個 IPA 文件(稍後您需要找到它)。

在 Firebase 控制台中運行測試

  1. 如果您還沒有,請打開Firebase 控制台並創建一個項目。

  2. 在 Firebase 控制台的測試實驗室頁面上,單擊運行您的第一個測試 > 運行 iOS 遊戲循環

  3. 上傳應用部分,點擊瀏覽,然後選擇您應用的 IPA 文件(如果您還沒有,請為您的應用生成一個 IPA 文件)。

  4. 可選:如果您想一次運行多個循環(又名場景)或選擇要運行的特定循環,請在場景字段中輸入循環編號。

    例如,當您輸入“1-3、5”時,Test Lab 會運行循環 1、2、3 和 5。默認情況下(如果您未在Scenarios字段中輸入任何內容),Test Lab 只會運行循環 1。

  5. Devices部分,選擇您要在其上測試您的應用程序的一台或多台物理設備,然後單擊Start Tests

使用 gcloud beta CLI 運行測試

  1. 如果您還沒有,請配置您的本地 gcloud SDK 環境,然後確保安裝gcloud beta 組件

  2. 運行gcloud beta firebase test ios run命令並使用以下標誌配置運行:

遊戲循環測試的標誌
--type

必需:指定要運行的 iOS 測試類型。您可以輸入測試類型xctest (默認)或game-loop

--app

必需:您應用的 IPA 文件的絕對路徑(Google Cloud Storage 或文件系統)。此標誌僅在運行遊戲循環測試時有效。

--scenario-numbers

您想要在您的應用程序中運行的循環(又名場景)。您可以輸入一個循環、一個列表或多個循環或一系列循環。默認循環為 1。

例如, --scenario-numbers=1-3,5運行循環 1、2、3 和 5。

--device-model

您要在其上運行測試的物理設備(找出您可以使用的可用設備)。

--timeout

您希望測試運行的最長持續時間。您可以輸入一個整數來表示持續時間(以秒為單位),或者輸入一個整數和枚舉以將持續時間表示為更長的時間單位。

例如:

  • --timeout=200強制您的測試在運行到 200 秒時終止。
  • --timeout=1h強制您的測試在運行長達一個小時時終止。

例如,以下命令運行遊戲循環測試,在 iPhone 8 Plus 上執行循環 1、4、6、7 和 8:

gcloud beta firebase test ios run
 --type game-loop --app path/to/my/App.ipa --scenario-numbers 1,4,6-8
 --device-model=iphone8plus

有關 gcloud CLI 的更多信息,請參閱參考文檔

在本地運行測試

要在本地運行您的測試,請在模擬器中加載您的遊戲應用程序並運行:

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

提前結束測試

默認情況下,遊戲循環測試會繼續運行,直到超時五分鐘,即使所有循環都已執行。達到超時時,測試結束並取消任何掛起的循環。您可以通過在應用程序的 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)
}

目標-C

- (void)finishLoop {
  UIApplication *app = [UIApplication sharedApplication];
  [app openURL:[NSURL URLWithString:@"firebase-game-loop-complete://"]
      options:@{}
completionHandler:^(BOOL success) {}];
}

您的遊戲循環測試終止當前循環並執行下一個循環。當沒有更多循環要運行時,測試結束。

編寫自定義測試結果

您可以配置遊戲循環測試以將自定義測試結果寫入設備的文件系統。這樣,當測試開始運行時,測試實驗室將結果文件存儲在測試設備(您必須自己創建)上的GameLoopsResults目錄中。測試結束時,測試實驗室將所有文件從GameLoopResults目錄移動到項目的存儲桶中。設置測試時請記住以下幾點:

  • 無論文件類型、大小或數量如何,都會上傳所有結果文件。

  • 直到測試中的所有循環都運行完畢後,測試實驗室才會處理您的測試結果,因此如果您的測試包含多個寫入輸出的循環,請確保將它們附加到唯一的結果文件或為每個循環創建一個結果文件。這樣,您可以避免覆蓋前一個循環的結果。

要設置測試以編寫自定義測試結果:

  1. 在您應用的Documents目錄中,創建一個名為GameLoopResults的目錄。

  2. 從您應用程序代碼的任何位置(例如,您的應用程序委託)添加以下內容:

    迅速

    /// 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.
      }
    }
    

    目標-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];
        }
    }