At this time, the public API provided by Xcode, the Simulator and the Swift Package Manager does not appear have any method callable from setUp()
and tearDown()
XCText
subclasses to "Reset Contents and Settings" for the simulator.
There are other possible approaches which use public APIs:
Application Code. Add some myResetApplication()
application code to put the application in a known state. However, device (simulator) state control is limited by the application sandbox ... which is not much help outside the application. This approach is OK for clearing application controllable persistance.
Shell Script. Run the tests from a shell script. Use xcrun simctl erase all
or xcrun simctl uninstall <device> <app identifier>
or similar between each test run to reset the simulator (or uninstall the app). see StackOverflow: "How can I reset the iOS Simulator from the command line?"
xcrun simctl --help
# Uninstall a single application
xcrun simctl uninstall --help
xcrun simctl uninstall <device> <app identifier>
# Erase a device's contents and settings.
xcrun simctl erase <device>
xcrun simctl erase all # all existing devices
# Grant, revoke, or reset privacy and permissions
simctl privacy <device> <action> <service> [<bundle identifier>]
- Xcode Schema Script Action. Add
xcrun simctl erase all
(or xcrun simctl erase <DEVICE_UUID>
) or similar commands to an Xcode Scheme section such as the Test or Build section. Select the Product > Scheme > Edit Scheme… menu. Expand the Scheme Test section. Select Pre-actions under the Test section. Click (+) add "New Run Script Action". The command xcrun simctl erase all
can be typed in directly without requiring any external script.
Options for invoking 1. Application Code to reset the application:
A. Application UI. [UI Test] Provide a reset button or other UI action which resets the application. The UI element can be exercised via XCUIApplication
in XCTest
routines setUp()
, tearDown()
or testSomething()
.
B. Launch Parameter. [UI Test] As noted by Victor Ronin, an argument can be passed from the test setUp()
...
class AppResetUITests: XCTestCase {
override func setUp() {
// ...
let app = XCUIApplication()
app.launchArguments = ["MY_UI_TEST_MODE"]
app.launch()
... to be received by the AppDelegate
...
class AppDelegate: UIResponder, UIApplicationDelegate {
func application( …didFinishLaunchingWithOptions… ) -> Bool {
// ...
let args = ProcessInfo.processInfo.arguments
if args.contains("MY_UI_TEST_MODE") {
myResetApplication()
}
C. Xcode Scheme Parameter. [UI Test, Unit Test] Select the Product > Scheme > Edit Scheme… menu. Expand the Scheme Run section. (+) Add some parameter like MY_UI_TEST_MODE
. The parameter will be available in ProcessInfo.processInfo
.
// ... in application
let args = ProcessInfo.processInfo.arguments
if args.contains("MY_UI_TEST_MODE") {
myResetApplication()
}
D. Direct Call. [Unit Test] Unit Test Bundles are injected into the running application and can directly call some myResetApplication()
routine in the application. Caveat: Default unit tests run after the main screen has loaded. see Test Load Sequence However, UI Test Bundles runs as a process external to the application under test. So, what works in the Unit Test gives a link error in a UI Test.
class AppResetUnitTests: XCTestCase {
override func setUp() {
// ... Unit Test: runs. UI Test: link error.
myResetApplication() // visible code implemented in application
gitlab-ci.yml
file. – Rightist