iOS Swift UI Tests (XCUITests) 稳定性问题 - fastlane scan

Posted

技术标签:

【中文标题】iOS Swift UI Tests (XCUITests) 稳定性问题 - fastlane scan【英文标题】:iOS Swift UI Tests (XCUITests) stability problem - fastlane scan 【发布时间】:2019-03-15 10:56:49 【问题描述】:

在通过 fastlane(扫描)运行 ios 测试期间,测试的稳定性会随机出现问题。日志中可见的错误:

Lost connection to the application (pid XXXX).
<unknown>:0

有谁知道与应用程序失去连接的原因可能是什么?

我的测试是在类中实现的,每个测试都继承了 BaseTest 类,应用程序以下列方式启动:

import XCTest

class BaseTest: XCTestCase 

    let app = XCUIApplication()
    ...

    override func setUp() 
        continueAfterFailure = false
        app.launch()
        ...
    

    override func tearDown() 
        super.tearDown()
        let screenshot = XCUIScreen.main.screenshot()
        let fullScreenshotAttachment = XCTAttachment(screenshot: screenshot)
        fullScreenshotAttachment.lifetime = .deleteOnSuccess
        add(fullScreenshotAttachment)
        app.terminate()
    

    ...

还有Page Object类实现,每个类都继承BaseScreen:

import XCTest

class BaseScreen 

    let app: XCUIApplication = XCUIApplication()


extension BaseScreen 
    func findAll(_ type: XCUIElement.ElementType) -> XCUIElementQuery 
        return app.descendants(matching: type)
    

测试通过 fastlane 运行如下:

lane :ui_tests do

    # Performing UI Tests
    scan(
      clean: true,
      reinstall_app: true,
      app_identifier: "XXX",
      workspace: workspace,
      scheme: "uiTests",
      devices: devicesUITests,
      code_coverage: true,
      open_report: false,
      output_style: "rspec",
      output_types: "html",
      output_directory: "./build/DerivedData/test_output",
      result_bundle: "true",
      buildlog_path: "./XXX",
      xcargs: "OTHER_SWIFT_FLAGS=\"-Xfrontend -debug-time-function-bodies\" SWIFT_OPTIMIZATION_LEVEL=\"-Owholemodule\"",
      derived_data_path: "./build/DerivedData"
      )

      puts("Generating Test Report ...")
      sh('xchtmlreport -r ../build/DerivedData/test_output')
      puts("Test Report Successfully generated")
  end

我也在使用 UIInterruptionMonitor 进行警报,这也可能是相关的。

已编辑: 在 StandardAndStandardError.txt 的结尾有以下片段:

2019-03-15 12:23:49.511 XXX[35711:8931440] *** Accessing the keyManager while not available yet
2019-03-15 12:23:49.511 XXX[35711:8931440] *** Accessing the keyManager while not available yet
2019-03-15 12:23:50.065 XXX[35711:8931461] Cannot find executable for CFBundle 0x7fd7f74336e0 </.../Library/Developer/CoreSimulator/Devices/.../data/Containers/Bundle/Application/.../XXX.app/Frameworks/DJISDK.framework/DJIFlySafe.bundle> (not loaded)
2019-03-15 12:23:50.073 XXX[35711:8931465] Cannot find executable for CFBundle 0x7fd7f75015d0 </.../Library/Developer/CoreSimulator/Devices/xxx/data/Containers/Bundle/Application/xxx/XXX.app/Frameworks/DJISDK.framework/SDKSharedLib.bundle> (not loaded)
CoreData: annotation:  Failed to load optimized model at path '/.../Library/Developer/CoreSimulator/Devices/xxx/data/Containers/Bundle/Application/xxx/XXX.app/GoogleMaps.bundle/GMSCacheStorage.momd/StorageWithTileProto.omo'
CoreData: annotation:  Failed to load optimized model at path '/.../Library/Developer/CoreSimulator/Devices/xxx/data/Containers/Bundle/Application/xxx/XXX.app/GoogleMaps.bundle/GMSCacheStorage.momd/StorageWithTileProto.omo'
CoreData: annotation:  Failed to load optimized model at path '/.../Library/Developer/CoreSimulator/Devices/xxx/data/Containers/Bundle/Application/xxx/XXX.app/GoogleMaps.bundle/GMSCacheStorage.momd/StorageWithTileProto.omo'
xxx(35711,0x7000060f9000) malloc: Heap corruption detected, free list is damaged at 0x6000009754a0
*** Incorrect guard value: 4416955976
xxx(35711,0x7000060f9000) malloc: *** set a breakpoint in malloc_error_break to debug

【问题讨论】:

您能分享更多日志吗?从一行日志中看不清楚,你分享了。来自 fastlane 的日志 + 来自派生数据的日志 - StandardOutputAndStandardError.txt 或 .crash 日志。代码(setUp 和 tearDown 以及您共享的其余代码)似乎没问题。 另外——你如何创建模拟器设备?你在devicesUITests 有什么设备?真机还是模拟器?您确定另一个进程(另一个 UI 测试/单元测试)不会使您的模拟器崩溃吗? fastfile 的唯一日志是丢失与应用程序的连接(pid XXXX)。我找到了 StandardOutputAndStandardError.txt,我觉得最后有一个有趣的片段,我编辑了帖子来展示它。 deviceUITests 列表目前仅包含一个设备:模拟器 iPhone 7。我确信另一个 UI 测试和单元测试不会导致任何崩溃。 这看起来像是框架的问题,您正在导入到您的 ui 测试目标。您可以尝试将 DJISDK 框架安装到 pod 中的 UI 测试目标。这应该会有所帮助。 【参考方案1】:

好吧,记得使用removeUIInterrutpionMonitor 方法!问题的原因是声明了许多 UIInterruptionMonitor 对象,并且线程存在问题。添加删除 UIInterruptionMonitor 对象后,问题消失了。 :)

【讨论】:

根据this documentation“XCTest 会在测试完成时移除添加的 UI 中断监视器,因此无需显式移除它们”【参考方案2】:

上周我遇到了类似的问题。我认为在我的构建机器上更新到最新版本的 fastlane 和 Xcode 解决了这个问题。

【讨论】:

以上是关于iOS Swift UI Tests (XCUITests) 稳定性问题 - fastlane scan的主要内容,如果未能解决你的问题,请参考以下文章

iOS/Swift 中的 UI 测试

更改UI标签栏宽度ios 13 swift

关于属性值 iOS/Swift 的 UI 测试

iOS/Swift:用于 UI 测试的“没有这样的模块...”

iOS / Swift(UI)始终运行的后台计算线程的高效实现-GCD案例?

IOS开发系列之Swift_UI_Btn