DBXCResultParser

DBXCResultParser 包提供了一个 Swift 模块,用于解析 Xcode 生成的 .xcresult 文件,并生成结构化的构建和测试结果报告。 它允许开发者以编程方式从 CI/CD 管道或自动化脚本中访问有关测试用例、代码覆盖率和警告的详细信息。

特性

用法

要在您的 Swift 包中使用 DBXCResultParser,请将其添加到您的 Package.swift 文件的依赖项中。

let package = Package(
    name: "YourPackageName",
    dependencies: [
        .package(url: "https://github.com/dodobrands/DBXCResultParser", .upToNextMajor(from: "3.0.0"))
    ],
    targets: [
        .target(
            name: "YourTargetName",
            dependencies: ["DBXCResultParser"]
        )
    ]
)

要解析 .xcresult 文件并访问报告数据,请使用 .xcresult 文件的路径初始化一个 DBXCReportModel

import DBXCResultParser

let xcresultPath = URL(fileURLWithPath: "/path/to/your.xcresult")
do {
    let reportModel = try DBXCReportModel(xcresultPath: xcresultPath)
    
    // Access different parts of the report:
    let modules = reportModel.modules
    let warningCount = reportModel.warningCount
    let totalCoverage = reportModel.totalCoverage
    
    // Iterate over modules, files, and tests:
    for module in modules {
        print("Module: \(module.name)")
        for file in module.files {
            print("  File: \(file.name)")
            for repeatableTest in file.repeatableTests {
                print("    Repeatable Test: \(repeatableTest.name)")
                for test in repeatableTest.tests {
                    print("      Test: \(test.status.icon) - Duration: \(test.duration)")
                }
            }
        }
    }
    
} catch {
    print("An error occurred while parsing the .xcresult file: \(error)")
}

使用 DBXCTextFormatter 格式化测试报告

DBXCTextFormatter 类提供了一种将 DBXCReportModel 中的数据格式化为人类可读字符串的方法。它支持两种输出格式:测试结果的详细列表和测试结果的摘要计数。

用法

要格式化您的测试报告数据,请创建 DBXCTextFormatter 的实例。

import DBXCResultParser

// Assuming you have already created a `DBXCReportModel` instance as `reportModel`
let reportModel: DBXCReportModel = ...

// Create a text formatter
let formatter = DBXCTextFormatter()

// Format the report data into a string
let formattedOutput = formatter.format(reportModel)

// Print the formatted output
print("Formatted Output:\n\(formattedOutput)")

format 方法还可以接受 DBXCReportModel.Module.File.RepeatableTest.Test.Status 的数组,以过滤哪些测试结果包含在输出中。默认情况下,它包括所有测试状态。

let formattedOutput = formatter.format(reportModel, include: [.failure])

输出格式

列表格式

输出测试结果的详细列表,包括每个文件的名称和每个测试的状态。

FileA.swift
✅ TestA1
❌ TestA2 (Failure reason)

FileB.swift
✅ TestB1
⚠️ TestB2

计数格式

输出测试结果的摘要计数,包括测试总数及其合并的持续时间。

12 tests (1m 23s)

自定义数字和测量格式

DBXCTextFormatter 允许您在格式化报告时指定一个区域设置。此区域设置用于根据提供的区域设置的约定来格式化数字和测量值。

let formatter = DBXCTextFormatter()
let output = formatter.format(reportModel, locale: Locale(identifier: "fr_FR"))
print(output) // Will output numbers and durations formatted in French

命令行工具用法

该软件包包含一个命令行工具,可以执行该工具来生成测试报告。以下是如何运行它的示例:

swift run DBXCResultParser-TextFormatterExec --xcresult-path path/to/tests.xcresult

可用选项包括:

许可证

此代码在 Apache License 下发布。 有关更多信息,请参见 LICENSE