You can add a "Run Script" phase to build phases in your test target to uninstall the app before running unit tests against it, unfortunately this is not between test cases, though.
/usr/bin/xcrun simctl uninstall booted com.mycompany.bundleId
Update
在测试之间,您可以删除该应用程序在拆卸阶段通过跳板。尽管如此,这确实需要使用 XCTest 的私有标头。 (标头转储可从Facebook 的 WebDriverAgent 在这里 https://github.com/facebook/WebDriverAgent/tree/master/PrivateHeaders/XCTest.)
以下是 Springboard 类中的一些示例代码,用于通过点击并按住从 Springboard 删除应用程序:
#斯威夫特 4:
import XCTest
class Springboard {
static let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
/**
Terminate and delete the app via springboard
*/
class func deleteMyApp() {
XCUIApplication().terminate()
// Force delete the app from the springboard
let icon = springboard.icons["Citizen"]
if icon.exists {
let iconFrame = icon.frame
let springboardFrame = springboard.frame
icon.press(forDuration: 1.3)
// Tap the little "X" button at approximately where it is. The X is not exposed directly
springboard.coordinate(withNormalizedOffset: CGVector(dx: (iconFrame.minX + 3) / springboardFrame.maxX, dy: (iconFrame.minY + 3) / springboardFrame.maxY)).tap()
springboard.alerts.buttons["Delete"].tap()
}
}
}
#斯威夫特 3-:
import XCTest
class Springboard {
static let springboard = XCUIApplication(privateWithPath: nil, bundleID: "com.apple.springboard")
/**
Terminate and delete the app via springboard
*/
class func deleteMyApp() {
XCUIApplication().terminate()
// Resolve the query for the springboard rather than launching it
springboard.resolve()
// Force delete the app from the springboard
let icon = springboard.icons["MyAppName"]
if icon.exists {
let iconFrame = icon.frame
let springboardFrame = springboard.frame
icon.pressForDuration(1.3)
if #available(iOS 13.0, *) {
springboard.buttons["Remove App"].tap()
springboard.alerts.buttons["Delete App"].tap()
springboard.alerts.buttons["Delete"].tap()
} else {
// Tap the little "X" button at approximately where it is. The X is not exposed directly
let xPosition = CGVector(dx: (iconFrame.minX + 3) / springboardFrame.maxX,
dy: (iconFrame.minY + 3) / springboardFrame.maxY)
springboard.coordinate(withNormalizedOffset: xPosition).tap()
springboard.alerts.buttons["Delete"].tap()
}
}
}
}
进而:
override func tearDown() {
Springboard.deleteMyApp()
super.tearDown()
}
私有标头已导入到 Swift 桥接标头中。您需要导入:
// Private headers from XCTest
#import "XCUIApplication.h"
#import "XCUIElement.h"
Note:从 Xcode 10 开始,XCUIApplication(bundleIdentifier:)
现在由Apple公开,私有标头是不再需要.