Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load files in xcode unit tests

I have an Xcode 5 unit test project and some test xml files related to it. I've tried a bunch of approaches but I can't seem to load the xml files.

I've tried the following which does not work

NSData* nsData = [NSData dataWithContentsOfFile:@"TestResource/TestData.xml"];

NSString* fileString = [NSString stringWithContentsOfFile:@"TestData.xml" encoding:NSUTF8StringEncoding error:&error];

Also if I try to preview all the bundles using [NSBundle allBundles] the unit test bundle does not appear in the list?

I tried to create a separate resource bundle and I can't seem to locate it programmatically although it does get built and deployed.

What am I doing wrong ?

like image 748
ArdenDev Avatar asked Oct 03 '13 05:10

ArdenDev


People also ask

How do I run unit test cases in Xcode?

⌘U will build and run all your test cases. It is the most commonly used shortcut when creating unit test cases. It is equivalent to ⌘R (build & run) while doing app development. You can use this shortcut to build your test target and run all the test cases in your test target.

How do I add test cases in Xcode?

To add a unit test target to an existing Xcode project, choose File > New > Target. Select your app platform (iOS, macOS, watchOS, tvOS) from the top of the New Target Assistant. Select the Unit Testing Bundle target from the list of targets.

How do I run a unit test file?

To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).


3 Answers

When running tests the application bundle is still the main bundle. You need to use unit tests bundle.

Objective C:

NSBundle *bundle = [NSBundle bundleForClass:[self class]]; NSString *path = [bundle pathForResource:@"TestData" ofType:@"xml"]; NSData *xmlData = [NSData dataWithContentsOfFile:path]; 

Swift 2:

let bundle = NSBundle(forClass: self.dynamicType) let path = bundle.pathForResource("TestData", ofType: "xml")! let xmlData = NSData(contentsOfFile: path) 

Swift 3 and up:

let bundle = Bundle(for: type(of: self)) let path = bundle.path(forResource: "TestData", ofType: "xml")! let xmlData = NSData(contentsOfFile: path)  
like image 188
sash Avatar answered Sep 27 '22 21:09

sash


With swift Swift 3 the syntax self.dynamicType has been deprecated, use this instead

let testBundle = Bundle(for: type(of: self)) guard let ressourceURL = testBundle.url(forResource: "TestData", ofType: "xml") else {     // file does not exist     return } do {     let ressourceData = try Data(contentsOf: ressourceURL) } catch let error {     // some error occurred when reading the file } 

or

guard let ressourceURL = testBundle.url(forResource: "TestData", withExtension: "xml") 
like image 23
MarkHim Avatar answered Sep 27 '22 19:09

MarkHim


As stated in this answer:

When the unit test harness runs your code, your unit test bundle is NOT the main bundle. Even though you are running tests, not your application, your application bundle is still the main bundle.

If you use following code, then your code will search the bundle that your unit test class is in, and everything will be fine.

Objective C:

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *path = [bundle pathForResource:@"TestData" ofType:@"xml"];
NSData *xmlData = [NSData dataWithContentsOfFile:path];

Swift:

let bundle = NSBundle(forClass: self.dynamicType)
if let path = bundle.pathForResource("TestData", ofType: "xml")
{
    let xmlData = NSData(contentsOfFile: path)
}
like image 23
stevo.mit Avatar answered Sep 27 '22 19:09

stevo.mit