Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 924
Noah Avatar asked Aug 03 '15 23:08

Noah


1 Answers

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.

like image 194
Duncan Dean Scholtz Avatar answered Oct 29 '22 15:10

Duncan Dean Scholtz