Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to use bundle resources for testing

Its convenient to use bundle resources for testing, for example to provide the expected outcome for a test.

For the old logic-style tests, I would use the main bundle for this, however for application style tests the main bundle is the app itself. I don't want to put test resources in the main bundle.

Eg, the following code is not working if the test resource only belongs to the test target:

//Load a resource from the main bundle
NSString* xml = [[JBBundleResource withName:@"signUpResponse.xml"] asString];

. . . is there a specific bundle for the tests? How can I get a handle to this?

like image 922
Jasper Blues Avatar asked Oct 22 '25 15:10

Jasper Blues


1 Answers

Just use:

NSBundle *testBundle = [NSBundle bundleForClass:[YourTestClass class]];

or in Swift 3:

let testBundle = Bundle(for: type(of: self))

in tests code. Everything you add in 'Copy Bundle Resources' in 'Build Phases' for your Tests target in XCode will be available in this bundle.

like image 53
Michał Ciuba Avatar answered Oct 24 '25 04:10

Michał Ciuba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!