苏。让我们把它分成更多的部分:
1/Should all of my test cases be in here or do I need more files?
嗯 - 你的测试与任何其他应用程序代码相同,你有。您是否将所有应用程序代码都放在一个文件中?可能不是,所以一个好的做法是将您的测试分成更多的类(我以他们测试的方式进行 - LoginTests 类,UserProfileTests 类等)。
更进一步 - 我将测试类和方法分成单独的文件 - f.e.我有一个方法,可以在 UI 测试中进行登录,所以我有 UITestCase+Login 扩展中的方法(UITestCase 是一个类,由所有这些 UITestCase+Something 扩展扩展)并且我有测试,即将在LoginTests 中进行登录,我从UITestCase+Login 扩展名中调用登录方法。
但是 - 您不一定需要更多的测试类 - 如果您决定将所有 UI 测试放在一个类中,那是您的选择。一切都会好起来的,但是将这些测试使用的测试和方法放在单独的文件中只是未来开发测试和方法的好习惯。
2/... Add additional security like secondary email address... How should these be ordered?
将它们排序为方法并在测试中调用它们。
当我使用无效的登录凭据时,这是我期待一些 UI 消息的方法:
func expectInvalidCredentialsErrorMessageAfterLoggingWithInvalidBIDCredentials() {
let alertText = app.alerts.staticTexts[localizedString("mobile.account.invalid_auth_credentials")]
let okButton = app.alerts.buttons[localizedString("common.ok")]
wait(
until: alertText.exists && okButton.existsAndHittable,
timeout: 15,
or: .fail(message: "Alert text/button are not visible.")
)
okButton.tap()
}
这是我在测试中的使用:
func testTryToLoginWitMissingBIDAndExpectError() {
let inputs = BIDLoginInputs(
BID: "",
email: "someemail@someemail.com",
departureIATA: "xxx",
dayOfDeparture: "xx",
monthOfDeparture: "xxx",
yearOfDeparture: "xxx"
)
loginWithBIDFromProfile(inputs: inputs)
expectInvalidCredentialsErrorMessageAfterLoggingWithInvalidBIDCredentials()
}
您可以看到,测试非常易读,并且它们(几乎完全)由方法组成,可以在更多测试中重复使用。
3/Within the test target, should you have multiple files?
再说一遍 - 这取决于您,但是将所有内容都放在一个文件中并不适合这些测试的维护和未来开发。
4/... Each time we run the test suite from the beginning the app starts in a state where you are not logged in...Right now I have it setup so that the login test runs once, then all other tests after it, then ends with logout...
不是一个好的方法(以我的卑微观点) - 将功能放入方法中(是的,我在这里重复自己:-))并将测试用例划分为更多文件(理想情况下是根据它们的功能性质,通过“他们做什么”)。
希望这对您有所帮助,当我开始进行 iOS UI 测试时,我也在同样的问题上遇到了很多困难。
哦。顺便说一句——我在 medium.com 上关于使用 XCTest 进行 iOS UI 测试的高级策略和方法的文章将在几天后发布,我可以在它发布后添加一个链接——这应该会进一步帮助你。