I have an application that makes use of core data. The behavior of the application is different depending on whether that data has been populated yet.
I had hoped that, before each test case, the core data would be emptied, making each test case run on a fresh app instance. Test cases shouldn't depend some test device's state, especially because other test cases manipulate this state. Unfortunately, by default, the core data lingers not only between test cases, but also between completely separate test runs. I'm trying to fix this behavior.
My first idea was to dump all core data in the teardown function. This way each test would clean up after itself. Unfortunately, I am unable to get an NSManagedObjectContext with this command:
let context = (UIApplication.sharedApplication().delegate as! MyApp.AppDelegate).managedObjectContext!
Because it has this compiler error:
Undefined symbols for architecture x86_64: "type metadata accessor for MyApp.AppDelegate", referenced from: MyAppUITests.MyAppUITests.tearDown
To fix this error I tried both adding the UITests to the AppDelegate target membership and doing an @testable import MyApp
. Neither worked. I need the managedObjectContext to empty the core data.
Now I'm thinking there has to be a better, built in, way to dump all core data at the beginning or end of each test case run. It would preferably be at the end, as each test should clean up after itself. Does anybody know how this could be done?
Thanks in advance!
You can do this easy by using XCUIApplication().launchEnvironment = ["key":"val"]
and checking if you AppDelegate and parsing NSProcessInfo.
I am battling to get information form the app to the test / sending instruction not only during startup.
来源:https://stackoverflow.com/questions/31798371/how-to-start-with-empty-core-data-for-every-ui-test-assertion-in-swift