ストーリーボードを使わないIOS開発(テスト)
実行方法
command + U or Project > Test
実行設定
再生ボタン2つ横のプロジェクト名ボタンをクリック > Edit Scheme > Test > Test > Info で実行するテストファイルにチェックを入れる
Unit Test
単体テストができる
表示テスト
let button = viewController.submitButton XCTAssertNil(button) // 見つからない XCTAssertNotNil(button) // 見つかる XCTAssertTrue(button.isHidden) // 非表示 XCTAssertEqual(button.titleLabel?.text, "登録") // イコール
UI Test
シュミレーターで動きを確認しながら結合テストができる?
検索
let app = XCUIApplication() app.buttons.staticTexts["OK"] // テキストがOKのボタンを特定 app.buttons["ok icon"] // 画像名から特定 app.tables.elementBoundByIndex(0) // 0番目の要素を取得 app.tabBars.elementBoundByIndex(1) // 1番目の要素を取得
イベントテスト
let button = viewController.submitButton button.sendActions(for: .touchUpInside) // イベント実行
フォーカステスト
let textField = viewController.textField textField.becomeFirstResponder() // フォーカス textField.resignFirstResponder() // アウト
操作
let button = XCUIApplication().buttons["ok icon"] button.tap() // タップ button.tapWithNumberOfTaps(5) //5回タップ button.pressForDuration(5) // 5秒タップ let app = XCUIApplication() app.textFields["name"].typeText("とんたん") // テキスト入力
その他
sleep(1) // 処理を1秒停止