首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在SwiftUI中使用UITest在多台设备上进行测试

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

Stack Overflow用户
提问于 2020-06-09 10:18:17
回答 1查看 437关注 0票数 1

我需要在几个iPhones和几个iPads上测试。我现在通过下面的菜单手动切换目标设备。

这项任务耗费了很多时间。所以我想自动切换目标设备。我现在使用的是Xcode11.5。你能告诉我怎么做吗?

代码语言:javascript
复制
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()
    }
}
代码语言:javascript
复制
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())
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-06-09 12:07:55

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

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

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

代码语言:javascript
复制
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'
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62273987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档