I am playing around with the new UI testing introduced in Xcode 7 beta. In one of my UI testing scenarios, I need to add some code that does the same thing as clicking Simulator -> Reset Content and Settings
in the setup()
of my test file, which is a XCTestCase
. Can the reset be done programmatically? Or, can we mimic the effect of a factory reset on an app in test code?
2 Answers
Not entirely programmatically, but you can always write a bash file to delete:
${user.home}/Library/Application Support/iPhone Simulator/${simulator.version}
That will clear the settings on the simulator.
My understanding is that you won't be able to that from within your app, as apps are sandboxed.

- 5,905
- 3
- 35
- 51
Usually people were using shell scripts or apple scripts. However, using hard reset is absolutely not necessary.
You shouldn't care about data in other apps, you should care only about the data in your app. You can always delete your app data (files, user defaults) in the beginning of your tests. So, why should you do a hard reset?
A better solution is mocking. If your test supposes that, for example, some variable in
NSUserDefaults
is not set, you don't have to care about the actual value stored there, just mock the method your implementation is using (e.g.objectForKey:
and let it returnnil
.

- 128,090
- 22
- 218
- 270
-
Reset content and settings provides a simple way to mimic a whole range of real life use cases. Not only does it wipe keychain data for me (which persists through app uninstalls), it also reliably presents the "Allow MyApp to access your camera?" or "Allow MyApp to send you notifications?" system popups, which never shows up again if user responded once. – SeaJelly Aug 31 '15 at 17:34
-
I need to be able to test against an empty contacts list, and no permissions for contacts to my app. – Jose Llausas May 31 '16 at 03:38