Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode Unit Testing - Accessing Resources from the application's bundle?

I'm running into an issue and I wanted to confirm that I'm doing things the correct way.

I can test simple things with my SenTestingKit tests, and that works okay. I've set up a Unit Test Bundle and set it as a dependency on the main application target. It successfully runs all tests whenever I press cmd+B.

Here's where I'm running into issues. I have some XML files that I need to load from the resources folder as part of the application. Being a good unit tester, I want to write unit tests around this to make sure that they are loading properly.

So I have some code that looks like this:

NSString *filePath = [[NSBundle mainBundle] 
    pathForResource:@"foo" ofType:@"xml"];

This works when the application runs, but during a unit test, mainBundle points to the wrong bundle, so this line of code returns nil.

So I changed it up to utilize a known class like this:

NSString *filePath = [[NSBundle bundleForClass:[Config class]] 
    pathForResource:@"foo" ofType:@"xml"];

This doesn't work either, because in order for the test to even compile code like this, it Config needs to be part of the Unit Test Target. If I add that, then the bundle for that class becomes the Unit Test bundle. (Ugh!)

Am I approaching this the wrong way?

like image 734
Ben Scheirman Avatar asked May 05 '10 17:05

Ben Scheirman


People also ask

How do I run unit test 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 unit tests to an existing Xcode project?

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.

Which framework is used to write unit test cases in Xcode?

Use the XCTest framework to write unit tests for your Xcode projects that integrate seamlessly with Xcode's testing workflow.


1 Answers

While trying to reproduce your problem it hit me: Have you tried just adding your resources to your test bundle? Would that cause any problems for you? I did exactly that, and it worked great for me. For example:

Code:

- (void)testBundleLoading {
  NSLog(@"Main Bundle Path: %@", [[NSBundle mainBundle] bundlePath]);

  for (NSBundle *bundle in [NSBundle allBundles]) {
    NSLog(@"%@: %@", [bundle bundleIdentifier], 
                     [bundle pathForResource:@"fire" ofType:@"png"]);
  }
}

Output:

2010-05-05 14:31:05.961 otest[16970:903] Main Bundle Path: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.3.sdk/Developer/usr/bin
2010-05-05 14:31:05.962 otest[16970:903] (null): (null)
2010-05-05 14:31:05.963 otest[16970:903] com.freetimestudios.KayakKingTests: /Volumes/FreeTime/KayakKing/build/Debug-iphonesimulator/KayakKingTests.octest/fire.png

I hope this helps.

like image 137
Nathan Eror Avatar answered Oct 02 '22 20:10

Nathan Eror