如何根据测试用例失败或跳过或通过xctest框架来触发另一个方法或任务

How to trigger another method or task based on testcase failure or skipped or pass in xctest framework

我正在使用 xctest 框架在 swift 中编写 UI 个测试用例。

我想根据测试用例通过或失败或跳过来触发另一项任务(例如截屏或)。我当前的代码如下

  import Foundation
  import XCTest

  class HomePageChromeUITests: XCTestCase {

var pathStr:String = ""
override func setUp() {

    super.setUp()
    continueAfterFailure = false
    self.launchWithUserLoggedIn()
}

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

func testHomePageChrome_10385() {
    let app = XCUIApplication()


    let backButton = app.buttons[AccessibilityIdentifiers.PageEditorBackButton]
    let editButton = app.buttons[AccessibilityIdentifiers.PageEditorEditButton]

    XCTAssertTrue(backButton.exists && backButton.frame.origin.x < editButton.frame.origin.x, "Back button does not exist.---10385")
    XCTAssertTrue(!editButton.exists, "Edit button does not exist.---10385")

 }
}

我想在测试用例失败时触发一个方法,并在那里传递我失败的消息,即“10385”。

我怎样才能做到这一点。任何帮助将不胜感激。

您应该子class XCTestCase 并覆盖函数recordFailure(withDescription:inFile:atLine:expected:) 然后让您的所有测试用例继承自这个新的class。

只要测试失败,就会调用此方法。

import XCTest
class SomeSubclass: XCTestCase {

override func recordFailure(withDescription description: String, inFile filePath: String, atLine lineNumber: Int, expected: Bool) {
    super.recordFailure(withDescription: description, inFile: filePath, atLine: lineNumber, expected: expected)
    //Take your screenshot here
}
}

class TestClass: SomeSubclass {
//Your tests..
}

在XCode9 Apple将其之前的XCTestObservation更改为XCTestObservationCenter。您会找到参考 here

要根据 TC 通过或失败做一些事情,您需要注册 testObserver 并将其添加到您的测试中。

这是完整的实现。

第 1 步:创建一个名为 observer 的 class。

 import Foundation
 import XCTest

 class Observer: NSObject, XCTestObservation {
var failedTC = 0

func testBundleWillStart(_ testBundle: Bundle) {
    print("Test bundle started")
}

func testBundleDidFinish(_ testBundle: Bundle) {
    print("Test bundle Finished")
}

func testSuiteWillStart(_ testSuite: XCTestSuite) {
    print("Test suite started")
}

func testSuiteDidFinish(_ testSuite: XCTestSuite) {
    print("Test suite ended")
}

func testSuite(_ testSuite: XCTestSuite, didFailWithDescription description: String, inFile filePath: String?, atLine lineNumber: Int) {
    print("Test suite failed, information: " + description)
}

func testCaseWillStart(_ testCase: XCTestCase) {
    print("Test case started")
}

func testCaseDidFinish(_ testCase: XCTestCase) {
    print("Test case finished")
}

func testCase(_ testCase: XCTestCase, didFailWithDescription description: String, inFile filePath: String?, atLine lineNumber: Int) {
    print("Test case failed. Message: " + description)

    var tmpMsgArr = description.components(separatedBy: ".---")
    let testcaseID = tmpMsgArr[0]

    print("------" + testcaseID)

    yourAwesomeMethodThatWillbeCalledWhenTCFails() // implement this method that you want to execute

    failedTC += 1
}

}

第 2 步:将此观察者添加到您的测试中。添加观察者代码在这里。

  let observer = Observer() // Your created observer class
  let observationCenter = XCTestObservationCenter.shared()
  observationCenter.addTestObserver(observer)

注意: 当你分解你的测试时移除观察者,否则如果你在每个 setUp 上添加一个观察者,你将得到多次调用和多次调用测试事件.

在你的情况下重写你的设置方法如下:

 override func setUp() {

    super.setUp()
    continueAfterFailure = false

    let observer = Observer() // your created observer class
    let observationCenter = XCTestObservationCenter.shared()
    observationCenter.addTestObserver(observer)

    self.launchWithUserLoggedIn()
}

希望这个完整的示例对您有所帮助。