如何基于测试用例失败触发另一个方法或任务,或者跳过或传入xctest框架

问题描述 投票:2回答:2

我正在使用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")

 }
}

我想在testcase失败时触发一个方法,然后在那里传递我失败的消息,即“10385”。

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

swift ios11 xcode9 xctest xcode-ui-testing
2个回答
4
投票

在XCode 9中,Apple将其普遍的xctestobservation更改为xctestobservationcenter。你会发现参考here

要根据TC通过或失败执行某些操作,您需要注册testobsevation并将其添加到测试中。

这是完整的实现。

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

 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 Bundle Ended")
}

func testSuite(_ testSuite: XCTestSuite, didFailWithDescription description: String, inFile filePath: String?, atLine lineNumber: UInt) {
    print("test Suit 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: UInt) {
    print("test case failed message" + description)

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

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

    yourmethodThatwillbeCalledWhenTCFail() // 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)

在您的情况下,重写您的设置方法,如下所示

 override func setUp() {

    super.setUp()
    continueAfterFailure = false

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

    self.launchWithUserLoggedIn()
}

希望这个完整的例子能帮到你。


0
投票

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

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

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..
}
© www.soinside.com 2019 - 2024. All rights reserved.