Bundle for test class returns main bundle in Xcode 10, and not unit tests bundle

ios ui testing best practices
xcode unit test
xctest tutorial swift 4
unit testing network code ios
xcode ui testing
ios integration testing
swift testing framework
ios unit testing by example

We have UnitTests that are hosted in application. To load test resources we use: Bundle(for: TestClass.self).path(forResource: "some-file", ofType: "json").

In Xcode 9 it returned something like this: /var/folders/4k/dzpnw8352jn_fl5hcc76pjs8k9qytq/T/com.apple.dt.XCTest/IDETestRunSession-707F04EA-2C21-4BA4-8730-00B0EC0F708F/UnitTests-2C97055A-9154-4521-9D12-7CF32FC38793/Session-UnitTests-2018-06-08_141829-RGxhj2.log

But in Xcode 10 Bundle(for: TestClass.self) returns application bundle path /Users/user/Library/Developer/CoreSimulator/Devices/118AACB9-486C-4E3B-94EA-257C3F082AE1/data/Containers/Bundle/Application/4234B280-E49C-4044-BAF4-CBFFEA8AFDB4/App.app

As these files are included to unit test target, they can not be found any more

Update:

Fixed in Xcode 10.1


This is likely a bug in Xcode 10/Swift 4.2 as there is another bug related to Bundle in the release notes. You should definitely file a radar to make sure the issue is known.

From Xcode 10 beta release notes

• The Foundation API Bundle.init(for: AnyClass) always returns the application bundle when used on a class that inherits from a generic class, even if that class itself is not generic. (40367300)

As a potential work around, you could consider using the testing target's bundle identifier.

To find the bundle identifier of the test target, look here:

Update

It seems like Bundle has been an ongoing problem with Xcode 10 (really it's Swift 4.2 that's having issues).

From Xcode 10.1 beta 2 release notes

• The Bundle class's init(for:) initializer now works consistently with Swift classes, including when not running on the latest operating system versions. (44489216)

Xcode 10 framework test: unable to find any xca |Apple Developer , The framework has logic tests, and embeds xcasset resources used can start looking at the differences between it and your main project. I checked, and MyClass.swift is only included in the framework target, not the unit test target. bundle identifier instead of Bundle(for:) with the class.self, I can force it  Now create unit a test bundle, as usual. Add a dependency to BundleTestRunner, as well as to the bundle you'd like to test. After adding tests, building will cause errors, though, because it's still using the Xcode test runner, instead of BundleTestRunner. Change the script in the "Run Script Phase" to:


This issue is now fixed in Xcode 10.1 as mentioned in the release notes Resolved Issues https://developer.apple.com/documentation/xcode_release_notes/xcode_10_1_release_notes

Writing Test Classes and Methods, Explains how to incorporate and use Xcode testing features for development projects. In part 2, I explained how to test a bundle by using a custom script in the "Run Script Phase". A slightly better approach is to use a test rig. From the Xcode documentation, a test rig is: A tool that can take information from its environment and will be passed the path to a unit test bundle as its sole argument.


After some class refactoring issue gone. Really in case if class contained URL or URLRequest property bundle was incorrect.

e.g. three classes declared in same module, and bundle is different:

class WithRequest {
    var request: URLRequest?
}

class WithURL {
    var url: URL?
}

class WithString {
    var string: String?
}

print(Bundle(for: WithRequest.self)) //Incorrect App Bundle
print(Bundle(for: WithURL.self)) //Incorrect App Bundle
print(Bundle(for: WithString.self)) //Correct bundle

iOS Unit Testing and UI Testing Tutorial, Learn how to add unit tests and UI tests to your iOS apps, and how you can Update note: Michael Katz updated this tutorial for Xcode 10.1, Swift 4.2, and iOS 12. When the test bundle appears in the Test navigator, click to open the Product ▸ Test or Command-U. Both of these run all test classes. In tests, create a “test dummy” version of the document class: a class with the same interface, but which doesn’t implement the behavior that makes it hard to test. In this case, a dummy document class should not interact with the file system.


Bug with Unit Tests on Mojave: "The bundle couldn't be loaded , Report Since I upgraded to macOS Mojave, I can't run unit test of modules I had to make sure not to set this setting for resource bundle targets though. but if I try to just run the same UI test target/class/testMethod, it occurs. Xcode 10 cannot run unit tests on mixed objc - swift framework project #8193. Unit-Testing is an important tool to validate the functionality. But I feel that it is not used to its full potential. Usually developers just test out the functions which takes in some arguments and returns some data model. Although such tests are usefull but they can’t help you to ascertain that the feature would definitely work if the tests are successfull.


Use bundleForClass to get the unit test bundle's path · GitHub, included inside app bundle, so `mainBundle` returns the app bundle iOS unit test bundle is not an executable bundle, it is invoked by NSBundle *testBundle = [NSBundle bundleForClass:[YourTestClass class]]; NSLog(@"path = %@", path); // path = /Users/nagano/Library/Developer/Xcode/DerivedData/OCTest-  When retrieving the main bundle from any application, the returned value might be NULL in the following situations: If a program is not bundled, attempting to get the main bundle might return a NULL value. The bundle code may try to create a main bundle to represent your program’s contents, but doing so is not possible in all cases.


Professional Xcode 3, Whether or not you subscribe to the principles of Extreme Programming, unit tests provide You then decide to add a new feature to some core class. Your tests are compiled into a unit test bundle, which is produced by a unit test target added to your project. Figure 10-1 shows the build log from a project with unit tests. As the name implies, in Unit Testing you create small and specific functionality tests for a certain unit of code and make sure that every unit passes the tests. If it passes the test, a green logo will appear next to it. If, for any reason, it was not successful, Xcode will mark a test as ‘failed’.