Xcode UI 测试示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31600777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:32:34  来源:igfitidea点击:

Xcode UI Test example

iosxcodeui-testingxcode-ui-testing

提问by Suragch

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.

我最近刚刚了解了Xcode 中的单元测试。现在我正在尝试 Xcode 7,当我创建一个新项目时,我看到有一个新的 UI 测试组。

enter image description here

在此处输入图片说明

I watched the WWDC 2015 videoand 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.

我观看了WWDC 2015 视频,它非常好,但是你有一个超级简单的例子可以让我自己完成吗?视频示例对我来说有点太复杂了。

Notes

笔记

回答by Suragch

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

使用单元测试来测试类中方法的有效性。您可以使用它们来测试您编写的代码。(请参阅在 Xcode 中设置简单单元测试的另一个示例。)

Use UI Teststo 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.

使用UI 测试来检查用户界面的有效性。把它想象成让你自己的机器人通过你的应用程序并与普通用户进行所有正常的交互。这可以节省您自己做的时间。

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.

在撰写本文时,很难访问 UI 组件的许多属性,但只需通过点击它们并滑动它们进行测试即可确认它们存在。

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.

这是我能想到的最简单的设置和 UI 测试:一个按钮,按下时会更改标签的文本。

Set up

设置

  • Create a new project in Xcode 7+ for iOS 9.0+.
  • Make sure that Include UI Tests is checked
  • 在 Xcode 7+ 中为 iOS 9.0+ 创建一个新项目。
  • 确保选中包括 UI 测试

enter image description here

在此处输入图片说明

  • 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 UILabeland a UIButtonto the storyboard

  • 如果您要向在 Xcode 7 之前创建的项目添加 UI 测试,请参阅此答案。(文件 > 新建 > 目标 > 测试 > Cocoa Touch UI 测试包)

  • 将 aUILabel和 a添加UIButton到故事板

enter image description here

在此处输入图片说明

  • Create an @IBOutlet and @IBAction in the ViewControllerand make the labeltext change when the buttonis pressed.

    import UIKit
    class ViewController: UIViewController {
        @IBOutlet weak var label: UILabel!
        @IBAction func button(sender: AnyObject) {
            label.text = "Hello"
        }
    }
    
  • 在 中创建@IBOutlet 和@IBActionViewControllerlabelbutton按下时更改文本。

    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.
  • 打开YourProjectUITests 文件。

enter image description here

在此处输入图片说明

  • Put your curser in the testExample()method. (You can delete the comments)
  • 将您的光标放在testExample()方法中。(你可以删除评论)

enter image description here

在此处输入图片说明

  • Press the red Record button
  • 按红色录制按钮

enter image description here

在此处输入图片说明

  • 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 staticTextlines 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.

  • 在应用程序中,(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"].tap()
        XCTAssert(app.staticTexts["Hello"].exists)
    }
    
  • 按左侧的菱形运行 UI 测试。当它通过时它应该变成绿色。

enter image description here

在此处输入图片说明

  • That's it! This showed that the UIButtonand UILabelexist 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.
  • 就是这样!这表明UIButtonUILabel存在并且标签的文本发生了变化。如果您想看到它失败(一个好主意),您可以将“Hello”更改为其他内容。

Further study

进一步研究

回答by Himanshu Garg

@Suragch+1 for the answer. One thing I observed and want to share that every function inside the UI Test case must start with "test". You can append extra name after that. Only this way the button(for clicking to start the test) appears.

@Suragch+1 的答案。我观察到并想分享的一件事是 UI 测试用例中的每个函数都必须以“test”开头。您可以在此之后附加额外的名称。只有这样按钮(点击开始测试)才会出现。