import XCTest
final class DJKAdvancedLearning_UITests: XCTestCase {
//把app的创建提取出来
let app = XCUIApplication()
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false
// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
//初始化创建
app.launch()
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testExample() throws {
// UI tests must launch the application that they test.
//不需要每个测试都去初始化一遍,直接用全局的app
// let app = XCUIApplication()
// app.launch()
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testLaunchPerformance() throws {
if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 7.0, *) {
// This measures how long it takes to launch your application.
measure(metrics: [XCTApplicationLaunchMetric()]) {
XCUIApplication().launch()
}
}
}
}
Swift中的UITest
UITest和UnitTest又完全不一样了,UITest可以测试用户的界面流程。而不会去测试所有的代码逻辑。
实现
创建步骤
和UnitTest一样在我们的要测试项目中 File->New->Targer->UnitTest Bundle->命名为(项目名_UITest)->Finish
初始Test文件
这里和UnitTest不一样的地方在于,不需要每个测试都去创建Application,只要在初始化的时候创建一次。
命名规则
这里参考UnitTest一样
编写用例
这里的代码是我通过XCode自带的模拟器操作来自动编写出来的,然后再自己修改这个用例,这个是原始的,操作如下:
https://user-images.githubusercontent.com/9426603/208841968-894d823e-7d21-41a7-a321-c02e6f6d6f09.mov
修改后的例子
一些小技巧
可以通过在View上添加accessibilityIdentifier来讲Test中的Name变为固定的控件,而不是通过Text的String来判断,不然如果这个String更改了就需要更改测试用例。
判断的话同样是使用XCTAssert()的一些方法来进行判断,增加了一些UI的判断比如:
当点击框出现和消失的时候,可能太快了测试会捕捉不到,这时候可以添加sleep(1)时间,或者等待超时来等待我们的控件显示或者消失
// sleep(1) 也可以使用sleep等待,如果控制不好时间的话还是超时比较方便 XCTAssertFalse(existAlert) XCTAssertFalse(alert.exists)
参考资料