72

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 ?

pkamb
  • 33,281
  • 23
  • 160
  • 191
ArdenDev
  • 4,051
  • 5
  • 29
  • 50
  • Hey where did you keep the file for it be accessible via the route "TestResource/TestData.xml"? – Bhargav Oct 25 '16 at 05:39

8 Answers8

121

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) 
sash
  • 8,423
  • 5
  • 63
  • 74
16

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")
Wane
  • 301
  • 3
  • 5
MarkHim
  • 5,686
  • 5
  • 32
  • 64
  • 1
    This shows better practice Swift than [the most popular answer](https://stackoverflow.com/a/23241781/1129789): no force unwraps, preferring URLs over paths, and using `Data` rather than `NSData`. – Luke Rogers Jul 24 '18 at 15:07
8

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)
}
Community
  • 1
  • 1
stevo.mit
  • 4,681
  • 4
  • 37
  • 47
3

Swift 4+

I found none of the answers here up–to date or using a URL.

First add the following function to your testClass:

/// getFile. Opens a file in the current bundle and return as data
/// - Parameters:
///   - name: fileName
///   - withExtension: extension name, i.e. "json"
/// - Returns: Data of the contents of the file on nil if not found
static func getFile(_ name: String, withExtension: String) -> Data? {
    guard let url = Bundle(for: Self.self)
            .url(forResource: name, withExtension: withExtension) else { return nil }
    guard let data = try? Data(contentsOf: url) else { return nil }
    return data
}

Then you can call it in your test class like this:

func test() throws {
    let xmlData = Self.getFile("TestData", withExtension: "xml")
    XCTAssertNotNil(xmlData, "File not found")
}

Note: Apple recommends that URL's should always be used for resources (not paths) for security reasons.

Sverrisson
  • 17,970
  • 5
  • 66
  • 62
2

Relative paths are relative to the current working directory. By default, that's / — the root directory. It's looking for that folder at the root level of your startup disk.

The correct way to get a resource that's within your bundle is to ask your bundle for it.

In an application, you'd get the bundle using [NSBundle mainBundle]. I don't know if that works in a test case; try it, and if it doesn't (if it returns nil or an unuseful bundle object), substitute [NSBundle bundleForClass:[self class]].

Either way, once you have the bundle, you can ask it for the path or URL for a resource. You generally should go for URLs unless you have a very specific reason to need a path (like passing it to a command-line tool using NSTask). Send the bundle a URLForResource:withExtension: message to get the resource's URL.

Then, for the purpose of reading a string from it, use [NSString stringWithContentsOfURL:encoding:error:], passing the URL you got from the bundle.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • 1
    So I found out that loading the bundle using its identifier worked. Although this identifier is not part of the array in [NSBundle allBundles]. Ideally it should list it ... – ArdenDev Oct 08 '13 at 03:37
1
  1. Get main bundle

  2. Use main bundle to get local mock data

  3. Load data from JSON

    let mainBundle = Bundle(identifier: "com.mainBundle.Identifier")
    if let path = mainBundle?.path(forResource: "mockData", ofType: "json") {
         do {
             let testData = try Data(contentsOf: URL(fileURLWithPath: path))
             networkTestSession.data = testData
         } catch {
             debugPrint("local json test data missing")
         }
     }
    
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
zdravko zdravkin
  • 2,090
  • 19
  • 21
1

One thing to note is(It waste me serval minutes to figure it out):

Don't forget to add the test file to "Copy Bundle Resources" section of Build Phases

Without it, you couldn't load the file successfully.

MartinZ
  • 175
  • 2
  • 12
-2

I know this specific question is asking for xml files, but if you're trying to do the same with json files, try this:

let url = Bundle.main.url(forResource: "data", withExtension: ".json") guard let dataURL = url, let data = try? Data(contentsOf: dataURL) else { fatalError("Couldn't read data.json file") }

Joseph Francis
  • 1,111
  • 1
  • 15
  • 26