Swift - Get Nsbundle for Test Target

Swift - Get NSBundle for test target?

The corresponding Swift code is

// Swift 3 and later:
Bundle(for: ClassInTestTarget.self)

// Swift 1, 2:
NSBundle(forClass: ClassInTestTarget.self)

ClassInTestTarget.self return the class object for the "ClassInTestTarget" class.

How do I reach Test Target Bundle Plist-file from production code under the Test Target?

in objC I used this:

to get the test bundle's plist:

    Class cls = NSClassFromString(@"YourOwnTestCaseClass");
if(cls) {
//the plist
id dict = [[NSBundle bundleForClass:cls] infoDictionary];

//the path to it (IF you need it)
id testPlistPath = [[NSBundle bundleForClass:cls] pathForResource:@"Info" ofType:@"plist"];
}

in swift I thought I could use allBundles

    let bundles = NSBundle.allBundles()
for bundle in bundles {
let val = bundle.objectForInfoDictionaryKey("TESTING")
if val != nil {
println(bundle.infoDictionary)
}
}

BUT NEITHER WORKS IN SWIFT


DIFFERENT SOLUTION

so I propose setting an environment var in the SCHEME for testing

Sample Image

then check it in your code like

let val = getenv("TESTING") //Val is a C String

better:

let dict = NSProcessInfo.processInfo().environment 
let testing : AnyObject? = dict["TESTING"] //get a swift object

NSURL to file path in test bundle with XCTest

In fact, the [NSBundle mainBundle] when running a UnitTest is not the path of your app, but is /Developer/usr/bin, so this will not work.

The way to get resources in a unit test is here: OCUnit & NSBundle

In short, use:

[[NSBundle bundleForClass:[self class]] resourcePath]

or in your case:

[[NSBundle bundleForClass:[self class]] resourceURL]

Why can't code inside unit tests find bundle resources?

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. (Presumably, this prevents the code you are testing from searching the wrong bundle.) Thus, if you add a resource file to the unit test bundle, you won't find it if search the main bundle. If you replace the above line with:

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *path = [bundle pathForResource:@"foo" ofType:@"txt"];

Then your code will search the bundle that your unit test class is in, and everything will be fine.

Easiest way to use bundle resources for testing

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.

How to access Swift files from main target in test target

Yes you need to add them in "Compile Sources". Alternatively you can select file in "Project Navigator" and change target membership in "Utility -> File Inspector" pane on right side of Xcode window.

Swift equivalent of [NSBundle bundleForClass:[self class]]

Never used, but I think it should be this:

Swift <= 2.x

NSBundle(forClass: self.dynamicType)

Swift 3.x

Bundle(for: type(of: self))

XCTest fails when calling [NSBundle mainBundle]

The simplest and cleanest way of fixing this issue is to partially mock the NSBundle class in your unit tests to return [NSBundle bundleForClass:[self class]] when you call [NSBundle mainBundle].

You may put this in your -setup method so that it is mocked for your entire test class:

static id _mockNSBundle;

@implementation MyTests

- (void)setUp
{
[super setUp];

_mockNSBundle = [OCMockObject niceMockForClass:[NSBundle class]];
NSBundle *correctMainBundle = [NSBundle bundleForClass:self.class];
[[[[_mockNSBundle stub] classMethod] andReturn:correctMainBundle] mainBundle];
}

@end

Nice and clean.

[Source]



Related Topics



Leave a reply



Submit