Unit Testing Migrations

Core Data has been changing a lot recently, and I first started learning and using Core Data when the first version of this book came out. I want to be sure that with all of the changes that migrating from any version of the app still works properly. Does anyone have a unit test setup to test this as well? If not, I’d love to see this in the next version of the book, or as a tutorial on the site @raywenderlich.

1 Like

In the past i’ve performed migration tests using .xcappdata. AFIK there are no tutorials around this but it’s pretty simple…

If you install an app on a physical device, you can head into Window > Devices in Xcode and select your installed app from the list. Tapping the settings cog then gives you the option to download the apps data as a .xcappdata file that you can then import into your project workspace in Xcode. This is basically just the documents/library folders of your app so you can “Show Package Contents” on this and modify/build it manually if you’d like.

Once you have this data, you can then use it in your Unit Tests by clicking “Manage Schemes > Your Scheme > Test” then specifying the “Test Application Data”.

Note that you can only do this once per scheme so if you have lots of migration tests to perform then you might have to make a scheme for each one and run them all.

Let me know if you have any questions :slight_smile:

Thanks for letting me know! I am using a shared folder though to share with the app extensions, I don’t think the package will download that. I’ll have to look into it so I can get older builds that I have archives for.

No worries!

Ahh yeah, I’ve never tried it but you’re probably right. Maybe you’ll need to have some sort of way to specify the path of the data store in your tests?

I’d be interested to know how you end up achieving this :+1: