How to start with empty Core Data for every UI Test Assertion in Swift?

我与影子孤独终老i 提交于 2019-12-04 11:42:43

问题


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!


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!