오늘은 저번 포스팅에 이어서 UI Test를 진행하는 방법에 대해 알아 보겠다.
먼저 Project의 General 탭 하단에 + 버튼을 눌러 UI Testing Bundle을 생성해 주면 된다.
이렇게 Test Bundle을 생성하게 되면 test 진행을 위한 swift 파일이 만들어지게 되는데
해당 파일에서 Test 코드를 작성하여 테스트를 진행하면 된다.
그런데 UI Test에서 봤던 testExample 메소드와는 차이점이 있는 걸 확인할 수 있다.
app을 launch 시키는 로직이 존재하고 있는데
UI Test를 위해서 app을 실행한다고 이해하면 되겠다.
func testExample() throws {
// UI tests must launch the application that they test.
let app = XCUIApplication()
app.launch()
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
이제 해당 메소드 내부에서 테스트 코드를 작성하면 되는데
손쉽게 UI Components의 인스턴스를 가져와서 UI Test를 진행할 수 있도록
자동으로 코드를 입력해주는 방법에 대해 소개하겠다.
xcode 하단을 보면 아래 이미지와 같이 recording 버튼이 있다.
해당 버튼을 누르게 되면 시뮬레이터가 실행 될 것이다.
(디바이스에 연결되어 있는 상태라면 디바이스에서 앱이 실행된다.)
다음 설명을 이어가지 전에
진행해 볼 테스트 요건을 설명 해보자면
상단 label에 간단한 문제를 노출 시킨 후
textField에 정답을 입력한 뒤 확인 버튼을 누르는 시나리오이다.
현재 레코딩 버튼을 눌러져 있는 상태이기 때문에
button이나 textField를 터치하면 해당 View의 인스턴스를 손쉽게 가져올 수 있다.
상단 label, 중간 textField, 하단 button을 차례대로 눌렀을때
아래와 같이 제스처에 대한 코드를 자동으로 입력되어있는 걸 확인할 수 있다.
func testExample() throws {
let app = XCUIApplication()
app.launch()
app.windows.children(matching: .other).element.children(matching: .other).element.children(matching: .other).element.children(matching: .textField).element.tap()
app.staticTexts["확인"].tap()
}
tap()을 제거 후 각 View의 인스턴스를 아래처럼 프로퍼티로 가지고 있을 수도 있다.
let textField = app.windows.children(matching: .other).element.children(matching: .other).element.children(matching: .other).element.children(matching: .textField).element
let button = app.staticTexts["확인"]
진행하고자 하는 테스트에서 필요한 View는 textField이다.
textField의 text가 문제의 정답과 일치하는지에 대한 테스트가 목적이기 때문이다.
아래와 같이 XCTUnwrap을 사용하여 옵셔널한 값을 언래핑 시켜주고
XCTAssertEqual을 사용해서 입력 값과 정답이 동일한지
assert 구문을 통해서 테스트를 진행해줬다.
func testExample() throws {
let app = XCUIApplication()
app.launch()
let textField = app.windows.children(matching: .other).element.children(matching: .other).element.children(matching: .other).element.children(matching: .textField).element
let button = app.staticTexts["확인"]
// textfield tap
textField.tap()
// text 입력
textField.typeText("3")
// get text
let text = try XCTUnwrap(textField.value as? String)
// button tap
button.forceTapElement()
// test
XCTAssertEqual(text, "3")
}
'IOS' 카테고리의 다른 글
[IOS] Unit Test (0) | 2022.07.24 |
---|---|
[IOS] Submodule 설정 (0) | 2022.02.05 |
[IOS] Fastlane (0) | 2021.10.10 |
[IOS] TableView (with xib) (0) | 2021.02.21 |
[IOS] TableView (with Storyboard) (0) | 2021.02.21 |