Swift UI测试 - XCTAssertEqual具有错误的行为

时间:2017-05-13 12:38:30

标签: ios swift unit-testing testing xcode-ui-testing

我目前正试图在我的iOS应用程序中进行一些测试,但我遇到了问题。

这是我的代码(我将在后面解释这个问题):

let passwordField = app.secureTextFields["Password"]
passwordField.tap()
passwordField.typeText("Password")

let password = passwordField.value as? String
guard let testPassword = password, !testPassword.isEmpty else {
    XCTFail("Authentication tests failed : password field is wrongly formatted.")
    return
}

XCTAssertNotEqual(password, "Random password")
XCTAssertEqual(password, "Password")

XCTAssertNotEqual 部分很好,但 XCTAssertEqual 不是。 这是错误:

XCTAssertEqual failed : (“Optional(“********”)”) is not equal to (“Optional(“Password”)”)

有没有人知道为什么会抛出这个错误?我想我必须"玩"事实上这是一个安全的文本,但我还没有找到任何答案。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

XCTAssertEqual没有任何问题。

第一个陈述

XCTAssertNotEqual(password, "Random password")

是对的,因为:

`*********` is not equal "Random password"

第二个因为您在控制台中看到的错误消息而失败。

就个人而言,我认为你在这里测试的逻辑有点搞砸了。首先,我认为XCUIElement没有将安全文本转换为普通文本的界面。

其次,为什么要验证textField中的内容?可以肯定的是,无论你输入什么内容,都没有必要测试本机iOS UI元素的基本功能。

如果您想测试身份验证,请尝试使用不正确的密码登录,而不是使用正确的密码登录,并测试您的回复BE