如何识别 fastlane 扫描中失败的测试用例以重新运行?

How to identify failed test cases in fastlane scan for rerunning?

作为将 XCode 的 UI 测试添加到我们的 CI 流程的一部分,我决定在构建失败之前实施重新 运行 失败测试, 由于测试框架的普遍不可靠性。

我可以通过使用 rescue 块来避免构建立即失败,这不是问题,并且报告文件(JUnit 和 HTML)的位置是固定的,因此可以用例如 File.readlines.

打开

junit 报告文件的格式是级联的测试套件 -> 测试套件 -> 测试用例 -> 失败消息(如果适用),然后退回足够远以供下一个元素使用。看起来很标准。

然而,那时我有点卡住了(这可能不是正确的道路;首先有没有规范的更好的方法来做到这一点?)。在 bash 中,我会使用 grep -B 来提取失败消息之前的行,这将是失败测试用例的名称,然后使用 awk 获取相关的文本片段。简单的两个管道 shell 命令,然后我可以使用 -only-testing 参数将其反馈到 scan/xcodebuild。

有没有一种方法可以在 Ruby 中同样轻松地做到这一点,或者有一个 junit 插件可以让我只读出个别失败测试用例的名称?

万一以后有人遇到这个问题:

  def parse_ui_test_report
doc = File.open("#{REPORT_LOCATION}/report.junit") { |f| Nokogiri::XML(f) }
node_tree = doc.css('testcase')
test_identifiers_to_rerun = node_tree.map do |node|
  # Test case nodes in the JUNIT XML doc don't have children unless they failed
  if node.first_element_child
    node.values.join('/').gsub('.','/')
  end
end.select {|str| !str.nil? && !str.empty? }

return test_identifiers_to_rerun
end