如何在SwiftUI中使用UITest在多个设备上进行测试?

问题描述 投票:0回答:1

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

enter image description here

这个任务耗费了很多时间,所以我想自动切换目标设备。所以我想自动切换目标设备。 我现在用的是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个回答
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'
© www.soinside.com 2019 - 2024. All rights reserved.