Reputation: 1700
I'm writing an iOS app with Core Data support. I write Core Data code first and test them directly in methods of AppDelegate. After having initial working code, I decide to move the Core Data test code to XCTest. This is the first time I use XCTest and I thought tests were running in a separate process than iOS app, but it's apparently not the case. For example, the test can access managed object context set up by app.
So my question is in which thread do XCTest run? Is it main thread? My experiments seem to indicate so. For example, the test access objects created in main thread (e.g., AppDelegate instance) and Xcode doesn't complain about it. If so, I wonder who schedule the test to run in main thread and how?
I see logs like the following in console but they don't indicate the underlying details.
2020-01-13 11:34:38.212244+0800 ... Launching with XCTest injected. Preparing to run tests.
2020-01-13 11:34:38.559808+0800 ... Waiting to run tests until the app finishes launching.
Thanks for any help.
Upvotes: 0
Views: 1164
Reputation: 535606
I thought tests were running in a separate process than iOS app, but it's apparently not the case. For example, the test can access managed object context set up by app.
The tests are in a bundle injected into the app. The tests, in turn, see the app as a bundle; that is why you import the app into the tests. (This is different from UITests, where the app runs in a separate app runner environment.)
So my question is in which thread do XCTest run? Is it main thread? My experiments seem to indicate so. For example, the test access objects created in main thread (e.g., AppDelegate instance) and Xcode doesn't complain about it. If so, I wonder who schedule the test to run in main thread and how?
Everything runs on the main thread. It is unclear what difficulty you find with that or what the surprise would be. The app runs, and its code runs, and the tests are some of that code.
Upvotes: 1