Xcode UI测试示例 [英] Xcode UI Test example

查看:151
本文介绍了Xcode UI测试示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚了解了



我看了




  • 如果要向UI添加UI测试在Xcode 7之前创建的项目,请参阅




    • ViewController中创建一个@IBOutlet和@IBAction 并在按下按钮时更改标签文本。

        import UIKit 
      class ViewController:UIViewController {
      @IBOutlet weak var label:UILabel!
      @IBAction func按钮(发件人:AnyObject){
      label.text =你好
      }
      }




    做测试




    • 打开 YourProject UITests文件。






    • 将你的光标放在 testExample()方法中。 (您可以删除评论)






    • 按红色记录按钮






    • 在应用程序中,(1)点击标签,(2)点击按钮,然后(3)再次点击标签。 (4)再次按录制按钮停止录制。应该为您自动生成以下代码:

        func testExample(){

      let app = XCUIApplication()
      app.staticTexts [Label]。tap()
      app.buttons [Button]。tap()
      app.staticTexts [Hello]。tap ()
      }


    • 使用 staticText 行作为制作 XCTAssert 的起点。现在你应该:

        func testExample(){

      let app = XCUIApplication()
      XCTAssert(app.staticTexts [Label]。exists)
      app.buttons [Button] .swap()
      XCTAssert(app.staticTexts [Hello]。exists)
      }


    • 按左侧的菱形运行UI测试。它通过时应该变为绿色。







    • 就是这样!这表明存在 UIButton UILabel ,并且标签的文本已更改。如果你想看到它失败(一个好主意),你可以将你好改为别的。



    进一步研究




    I have just recently learned about Unit Testing in Xcode. Now I am trying out Xcode 7 and I see there is a new group for UI Tests when I create a new project.

    I watched the WWDC 2015 video and it was pretty good, but do you have a super simple example that I could go through myself? The video examples were a little too complex for me.

    Notes

    解决方案

    Use Unit Tests to test the validity of methods in your classes. You use them to test the code you have written. (See my other example for setting up a simple Unit Test in Xcode.)

    Use UI Tests to check the validity of the User Interface. Think of it like having your own robot to go through and do all the normal interactions with your app that a normal user would. This saves you the time of doing it yourself.

    At the time of this writing, it is difficult to access many of the properties of the UI components, but just having a test go through tapping them and swiping them confirms that they are there.

    Example

    This is about the simplest setup and UI test that I could think of: a button that when pressed changes the text of a label.

    Set up

    • Create a new project in Xcode 7+ for iOS 9.0+.
    • Make sure that Include UI Tests is checked

    • If you are adding UI tests to a project created before Xcode 7, see this answer. (File > New > Target > Test > Cocoa Touch UI Testing Bundle)

    • Add a UILabel and a UIButton to the storyboard

    • Create an @IBOutlet and @IBAction in the ViewController and make the label text change when the button is pressed.

      import UIKit
      class ViewController: UIViewController {
          @IBOutlet weak var label: UILabel!
          @IBAction func button(sender: AnyObject) {
              label.text = "Hello"
          }
      }
      

    Do the test

    • Open the YourProjectUITests file.

    • Put your curser in the testExample() method. (You can delete the comments)

    • Press the red Record button

    • In the app, (1) tap the label, (2) tap the button, and then (3) tap the label again. (4) Press the Record button again to stop recording. The following code should have been automatically generated for you:

      func testExample() {
      
          let app = XCUIApplication()
          app.staticTexts["Label"].tap()
          app.buttons["Button"].tap()
          app.staticTexts["Hello"].tap()
      }
      

    • Use the staticText lines as a starting point for making an XCTAssert. Now you should have:

      func testExample() {
      
          let app = XCUIApplication()
          XCTAssert(app.staticTexts["Label"].exists)
          app.buttons["Button"].tap()
          XCTAssert(app.staticTexts["Hello"].exists)
      }
      

    • Press the diamond on the left to run the UI Test. It should turn green when it passes.

    • That's it! This showed that the UIButton and UILabel exist and that the text of the label changed. If you want to see it fail (a good idea), you can change "Hello" to something else.

    Further study

    这篇关于Xcode UI测试示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆