Link XCTest framework:
After you update Xcode to version 6, when create a new test bundle in your project, system will automatically link XCTest for it.
But because XCTest framework is in a different location from other frameworks,
(Under Applications, right click “Xcode”, select “Show Package Contents” to enter the folder.)
That is to say, if you have an existing test target using test framework, and you want change to XCTest, you are not able to add XCTest in building phases “link binary with libraries”.
If you have an existing test target using XCTest, after Xcode update to version 6, you may now get an error message, can not find the underlying bundle, something, I don’t remember the exact message content. (I’ll fix this later~). The reason is that the XCTest framework is not under the folder where it used to be.
Add test target into the scheme:
- Click your “set the active scheme” button, it’s on the top left of Xcode, next to the “stop” button;
- Select “Edit Scheme” from the drop-down menu;
- Select “Test” from the left panel of the drop-down scheme edit window,;
- Select “Info” from the right panel;
- If there is no any target there, just click the “+” button and select the test target you want to use.
Write test code with Swift for Objective-C class
If there are only Swift files in you app module, you just need to import the app module into any of your test class.
If there are only Objective-C files in you app module, but you want to write some test classes in Swift, you need to create a bridging header for your test module. (Learn more about how to create a bridging header, please check out my post: Objective-C and Swift interoperation But here, the bridging header file name should start with your test module name.)
If you have both Swift and Objective-C files in your app module, and let’s say, it also has a bridging header, now you cannot import the app module. You might wonder why can not just import the app module? Well, I also expected this, but so far, it’s gonna give a error message: “Failed to import bridging header …”. So, we still need a bridging header for your test module. ( Issue: not able to access Objective-C third party library headers. I’m trying to figure this out, and will update this post after that. )
Write Test Cases with Objective-C for Swift class
No, according to Apple’s Xcode6 release notes, in the testing part, “XCTest test classes written in Objective-C cannot import the Swift generated interfaces header (
$(PRODUCT_MODULE_NAME)-Swift.h) for application targets, and cannot be used to test code that requires this header.” So, we’re not able to write Objective-C test cases for Swift class.