1

I'm trying to save files generated while running unit tests that are later loaded through the same tests on a different simulator/at a different time. I can't use the NSDocumentsDirectory because it changes every time I run the app on the same simulator. What shared directory can I write to or what path can I write to where the files can be loaded between different test runs?

Notes:

  • The recorded data is being recorded as the tests run. I'm recording http traffic and writting it to files to be loaded later.

  • I'm running all the tests on the same simulator, so if the directory isn't accessible between simulators that's fine. I want it to be accessible by the same simulator on different runs.

Minimi
  • 921
  • 10
  • 29

2 Answers2

2

When an iOS app writes to its documents directory on the iPhone simulator (NSDocumentDirectory), the data remains available to subsequent runs. Even if the app gets rebuilt/reinstalled, the data will remain with the app, but in the new NSDocumentDirectory. Even though the root app directory is different, the documents are automatically moved to the new NSDocumentDirectory.

So it just works, so long as its on the same simulator, in the same way it does if it is on the same device. You don't need to do anything.

I found that the trick when using the Mac OS X Finder or a terminal (for me) was to stop looking in the same absolute directory all the time, and get the app to log whatever the current documents directory is, and then check for the existence of the documents there in that new directory. I found that the system always moved documents there for me every time I rebuilt/reinstalled the app.

It appears to be much the same behaviour as when updating an app on a real device from the App Store, as far as I can tell. I guess it installs the new app, then moves all data from the old app to the new app, then deletes the old app.

Son of a Beach
  • 1,733
  • 1
  • 11
  • 29
1

Simulators are like separate devices: they can't share files, and neither can new installs (from a rebuild) of your app.1 I think you'll have to generate the files, and then find them manually in the simulated app container.

Then you can add the files to your test target in the "Copy bundle resources" build phase, then access them via NSBundle. The main bundle will still be the application even when testing, but you can find the right bundle with +bundleForClass: and a class that's only present in your test target.


1Son of a Beach's answer points out that this is wrong.

Community
  • 1
  • 1
jscs
  • 63,694
  • 13
  • 151
  • 195
  • The files are generated during the tests so that makes this difficult. The files are http traffic that I'm grabbing from requests and then writing to files. Next time the tests run I use the recorded responses if they exist. So I can't add them to the copy files build phase. – Minimi Dec 14 '16 at 23:53
  • I believe you will have to copy the files manually, then. Each simulator represents a separate device, and your app is sandboxed on each just as on a real device. – jscs Dec 14 '16 at 23:55
  • How do you mean manually? So I'm going to have to copy paste the files into the directory that hasn't yet been created because the tests haven't run yet? – Minimi Dec 14 '16 at 23:56
  • If you dig the files out of their location in [the simulated app container](http://stackoverflow.com/questions/1108076/where-does-the-iphone-simulator-store-its-data), you can then add them to the next build. – jscs Dec 14 '16 at 23:59
  • What do you mean add them to the build? So I get the responses I want by running the tests once and getting them out of that directory, I then add them to the build? I don't get this step – Minimi Dec 15 '16 at 00:01
  • Exactly, once you have the files from the first run of tests, you can use the suggestion in my answer: add those to the test target and read them from the bundle. – jscs Dec 15 '16 at 00:05
  • Alright sounds good. Thanks for the help, if you can think of a way to automate the manual copy/paste process or anything like that I'd love to hear! – Minimi Dec 15 '16 at 00:06
  • Glad I could help; wish I had a better answer. Maybe somebody else will come along in a few hours with another idea. – jscs Dec 15 '16 at 00:07
  • Wait here's a quick question, if I were to run the tests on the same simulator would there be a shared directory I can access? – Minimi Dec 15 '16 at 00:07
  • And by 'copy files' do you mean the 'copy bundle resources' build phase for the test target? – Minimi Dec 15 '16 at 00:20
  • 1
    You should be able to share between runs on the same sim as long as you don't rebuild/reinstall, yes. – jscs Dec 15 '16 at 00:32
  • And yes, sorry, "Copy bundle resources". – jscs Dec 15 '16 at 00:34