@testable Import

July 6th, 2015

Filed under: Xcode | Be the first to comment!

A new feature added in Xcode 7 to simplify unit testing is the @testable import statement. Instead of making your application classes members of the unit testing target, which is what you had to do to unit test Swift code in Xcode 6, add the following statement to your unit testing classes:

@testable import MyApp

Where MyApp is the name of your application target.

If you get link errors when running your tests, make sure the value of the Product Module Name build setting matches the module name in the @testable import statement. In my example that would be MyApp.

I was getting link errors in my project because my project name has a space in it so Xcode was using My_App as the module name in the Product Module Name build setting. My_App is not the same as MyApp, causing the link error. Changing the Product Module Name to MyApp fixed the link error.

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *