【问题标题】:How to test on several devices with UITest in SwiftUI如何在 SwiftUI 中使用 UITest 在多个设备上进行测试
【发布时间】:2020-09-28 02:55:42
【问题描述】:

我需要在几部 iPhone 和几部 iPad 上进行测试。我现在通过以下菜单手动切换目标设备。

此任务会消耗大量时间。所以我想自动切换目标设备。我现在正在使用 Xcode 11.5。你能告诉我怎么做吗?

import SwiftUI

struct ContentView: View {
    @State var flag = false

    var body: some View {
        VStack {
            Text(flag ? "Apple" : "Peach")
            .accessibility(identifier: "Text")
            Button(action: {
                self.flag.toggle()
            }) {
                Text("Update")
            }
            .accessibility(identifier: "Button")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
import XCTest

class swiftui_playgroundUITests: XCTestCase {

    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.
    }

    override func tearDownWithError() throws {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }

    func addScreenshot(screenshot: XCUIScreenshot) {
        let attachment = XCTAttachment(screenshot: screenshot)
        attachment.lifetime = .keepAlways
        add(attachment)
    }

    func testExample() throws {
        // UI tests must launch the application that they test.
        let app = XCUIApplication()
        app.launch()

        // Use recording to get started writing UI tests.
        // Use XCTAssert and related functions to verify your tests produce the correct results.
        let text = app.staticTexts["Text"]
        XCTAssertEqual(text.label, "Peach")
        addScreenshot(screenshot: app.windows.firstMatch.screenshot())

        let button = app.buttons["Button"]
        button.tap()

        XCTAssertEqual(text.label, "Apple")
        addScreenshot(screenshot: app.windows.firstMatch.screenshot())
    }
}

【问题讨论】:

    标签: ios xcode swiftui xctest


    【解决方案1】:

    您可以设置 Xcode 服务器以进行持续集成。在那里,您可以指定测试应在哪些设备上运行并手动、提交或计划执行测试。

    或者您可以通过终端运行测试并指定多个目标设备。

    我使用下面的代码在多个设备上运行我的 UI 测试计划并自动为 App Store 创建屏幕截图

    xcodebuild test -testPlan ScreenshotTests -project Sensor-App.xcodeproj -scheme Sensor-App \
    -destination 'platform=iOS Simulator,name=iPhone 8 Plus,OS=13.4'  \
    -destination 'platform=iOS Simulator,name=iPhone 11 Pro,OS=13.4' \
    -destination 'platform=iOS Simulator,name=iPhone 11 Pro Max,OS=13.4'  \
    -destination 'platform=iOS Simulator,name=iPad Pro (12.9-inch) (4th generation),OS=13.4'
    

    【讨论】:

      猜你喜欢
      • 2020-03-06
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 1970-01-01
      • 2015-07-27
      • 2012-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多