iPhone - Retrieving Resources for logical unit tests iPhone - Retrieving Resources for logical unit tests xcode xcode

iPhone - Retrieving Resources for logical unit tests


Ok, so I've figured it out. In order to open a file in a unit test, you'll need to specify the file to open as:

NSString * filePath = [[NSBundle bundleForClass:[self class] ] pathForResource:@"SimpleTestList" ofType:@"plist"];

If you include this in a class that's compiled as part of your unit test bundle, that class will look inside the unit test bundle for the file SimpleTestList.plist.

For a unit test, just make sure you set up "Copy Bundle Resources" to include your plist in your unit test bundle.


If you need the application delegate, you have to run the unit tests on the device itself and not the simulator. Also, you will see unit test output appear in the console, not in the build results.

The key thing to know is that there are two types of unit tests - logic tests that are run outside of the executable, and then integrated system kinds of tests that need the full running environment.

The logic tests MUST be run with the simulator selected as the target or they will not run.

The integrated system tests MUST be run as part of the executable, on the device - you'll want a new target to accomplish this.

Sorry this is all so complex, this aspect is still very much a work in progress compared to many other unit testing frameworks.


The Swift 3 translation of Gary's answer above (using an URL instead of a string path) is:

let url = Bundle(for: type(of: self)).url(forResource: "SimpleTestList", withExtension: "plist")

Notice the critical and non-obvious part type(of: self) instead of [self class].