2016-07-30 121 views
1

我正在开发testing framework for iOS development。我也希望这个测试框架经过充分测试。问题是,我无法弄清楚如何为测试目标编写一个测试,该测试声明我的框架正确地导致失败的测试。如果我创建一个失败的测试,我反过来会导致测试失败(我知道,这很混乱)。如何编写自定义XCTest断言的自动化测试?

考虑一个例子。我的框架的一部分包含函数来验证特定的代码段没有任何中断约束。

MTKAssertNoBrokenConstraints { 
    // UI code that might break some constraints 
} 

我已经手工测试此,以验证当没有损坏的限制,断言通过,但是当有破限制,它正确地标志着试验为不合格。

但我需要一个方法来验证MTKAssertNoBrokenConstraints马克测试为失败没有实际标志着测试这个本身失败。

我已经研究过创建一个符合XCTestObservation的自定义对象,但到目前为止我只能以无限递归结束。我不确定这是否是正确的路径,或者解决无限递归是否实际上将我带到了我需要的地方。

+0

回答此问题将帮助我解决[问题#2](https://github.com/metova/MetovaTestKit/issues/2)。我会很感激这里的答案,解决这个问题的请求,或者两者兼而有之。 – nhgrif

回答

1

以下测试拦截XCTFail("FOO")的故障,然后对故障执行一些检查。

class TestTheTests: XCTestCase { 

    var interceptFailure = false 
    var failureCount = 0 
    var failureDescription = "" 
    var failureFilePath = "" 
    var failureLineNumber: UInt = 0 
    var failureExpected = false 

    override func recordFailureWithDescription(description: String, inFile filePath: String, atLine lineNumber: UInt, expected: Bool) { 
     if !interceptFailure { 
      super.recordFailureWithDescription(description, inFile: filePath, atLine: lineNumber, expected: expected) 
     } else { 
      failureCount += 1 
      failureDescription = description 
      failureFilePath = filePath 
      failureLineNumber = lineNumber 
      failureExpected = expected 
     } 
    } 

    func testExample() { 
     interceptFailure = true 
     XCTFail("FOO") 
     interceptFailure = false 

     XCTAssertEqual(failureCount, 1) 
     XCTAssertTrue(failureDescription.hasSuffix("FOO"), "Was \"\(failureDescription)\"") 
    } 

} 
+0

好吧,这给了我很多继续。我会寻找一个更简洁的实现方式,而不是简单地设置一个标志,但我喜欢这个。 – nhgrif