XCUITest 与 TestRail 集成

Dim*_*mu4 4 ios xctest testrail swift xcuitest

目前正在将我的 UITest 运行结果集成到 TestRail 中,因此每次测试运行后,它都会在 testrail 中将我的测试标记为通过/失败。

我的想法是:

  1. 在 CI 中创建一个“预构建”脚本,该脚本将在 testrail 中创建测试运行。
  2. 在自动化执行过程中,在测试tearDown()中获取测试结果(如果测试失败或失败),将其全部保存到json文件中。-这是第一个问题,如果测试失败了怎么办?
  3. 完成所有测试后,运行“构建后”脚本以获取更新的 json 文件并向测试轨发送请求(这将标记通过\失败测试)

任何已经从事过此工作的人,这听起来适合您吗?有什么建议吗?

测试示例:

import XCTest

class MyUITests: XCTestCase {

    override func setUp() {
        super.setUp()
        continueAfterFailure = false
        appEntry.app.launch()
        dismissSystemAlerts()
    }

    override func tearDown() {
        super.tearDown()
    }

    func test_Elements() {
        // MARK: Sample test
        // my test actions are here
    }
}
Run Code Online (Sandbox Code Playgroud)

Dim*_*mu4 5

我就是这样实现的。首先,我在 CI 中有预构建脚本,它将在 TestRail 中创建新的测试运行。然后 UITestObserver 会向 TR 发送 API 来更新状态。

我添加的新课程:

import Foundation
import XCTest

class UITestObserver: NSObject, XCTestObservation {
    // Handle Test Case Failure
    public func testCase(_ testCase: XCTestCase,
                         didFailWithDescription description: String,
                         inFile filePath: String?,
                         atLine lineNumber: Int) {
        var testCaseId: [String] = []
        if let MyTestCaseID = testCase as? BaseTest { testCaseId = MyTestCaseID.inegrateTestRailId() }
        if testCaseId != ["NA"] {
            postTestRailAddResultAPI(for: testCase, testCaseId: testCaseId, description: description)
        }
    }

    // Handle Test Case Pass
    public func testCaseDidFinish(_ testCase: XCTestCase) {
        let testRun = testCase.testRun!
        let verb = testRun.hasSucceeded
        var testCaseId: [String] = []
        if let MyTestCaseID = testCase as? BaseTest { testCaseId = MyTestCaseID.inegrateTestRailId() }
        if verb == true && testCaseId != ["NA"] {
            postTestRailAddResultAPI(for: testCase, testCaseId: testCaseId, description: "PASS")
    }
}
Run Code Online (Sandbox Code Playgroud)

在 BaseTest setup 中添加了这一行:

XCTestObservationCenter.shared.addTestObserver(UITestObserver())
Run Code Online (Sandbox Code Playgroud)

并实现了 postTestRailAddResultAPI 发送实际请求以更新状态的函数。我现在所有的测试都testCaseId存储了 的值TestRail TestCase number,这就是它知道要更新哪些 TC 的方式。