Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何检查XCTestCase测试是否失败_Swift_Xctest_Xctestcase - Fatal编程技术网

Swift 如何检查XCTestCase测试是否失败

Swift 如何检查XCTestCase测试是否失败,swift,xctest,xctestcase,Swift,Xctest,Xctestcase,是否可以在运行测试中检查其任何xctaserts是否失败?我有一个测试,一行中有几个断言,我想在测试之后添加一些代码,以便在任何一个断言失败时执行特定操作: class testClass : XCTestCase { func testSomething() { let someComputedValue1 = func1() let someComputedValue2 = func2() XCTAssertLessThanO

是否可以在运行测试中检查其任何
xctasert
s是否失败?我有一个测试,一行中有几个断言,我想在测试之后添加一些代码,以便在任何一个断言失败时执行特定操作:

class testClass : XCTestCase
{
    func testSomething()
    {
        let someComputedValue1 = func1()
        let someComputedValue2 = func2()

        XCTAssertLessThanOrEqual(someComputedValue1, 0.5)
        XCTAssertLessThanOrEqual(someComputedValue2, 0.2)

        if anyOfTheAboveAssertionsFailed {
            performAction()
        }
    }
}

我想要提示的部分是,
以上任何一个assertionsfiled
条件都不会重复与硬编码值的比较。

您当然可以编写一个新函数

func assertLessThanOrEqual(value: Double, limit: Double) -> Bool {
    XCTAssertLessThanOrEqual(value, limit)
    return value <= limit
}

虽然使用您自己的断言方法可以解决PO的问题,但如果您需要使用多个XCAssert方法,则会很麻烦

另一种方法是覆盖
故障后继续
。如果没有失败,则不会请求属性。如果有,它会的

class MyTest: XCTest {
    private var hasFailed = false
    override var continueAfterFailure: Bool {
        get {
            hasFailed = true
            return super.continueAfterFailure
        }
        set {
            super.continueAfterFailure = newValue
        }
    }
    override func tearDown() {
        if hasFailed { performAction() }
        hasFailed = false
    }
}

注意,您可以替代另一个API以获得相同的效果:
func recordFailure(with description description:String,infle filePath:String,atLine lineNumber:Int,expected:Bool)
class MyTest: XCTest {
    private var hasFailed = false
    override var continueAfterFailure: Bool {
        get {
            hasFailed = true
            return super.continueAfterFailure
        }
        set {
            super.continueAfterFailure = newValue
        }
    }
    override func tearDown() {
        if hasFailed { performAction() }
        hasFailed = false
    }
}