I've been struggling all morning to add a working test target to my Swift project and now I think I finally got it working. So, I thought I'd share it with anyone interested:
- Start by setting "Defines module" in your .xcodeproj to "YES" (Choose your project, then "Build Settings" header, then "Defines Modules" line, then select "Yes").
- Add a test target. This is done in the "Test navigator" (a tab on the left hand side in the same space as the "Project navigator") by clicking the + symbol at the bottom.
- Edit the "Objective-C Bridging Header" path. With the default settings the test target will fail to import the bridging-header and other header files. To fix this, select your project in the projects navigator. Choose your iOS target -> Build Settings. Type "Bridging Header" in the search field. Copy the path (should be "Source/Bridging-Header.h" or something like that). Then, choose your test target and add the path in the test targets "Objective-C Bridging Header"-field. Now the test target should be able to find the bridging header.
- Add "Header Search Paths". Even if the test target now finds the bridging header, it'll still not find all other headers needed. To fix this, repeat step 3, but this time copy all values in the iOS targets "Header Search Paths"-field (should be a number of paths looking something like this: "Source/libs/cocos2d-iphone/cocos2d") and add them to the test targets "Header Search Paths".
- Almost done. In order to use your classes in your tests you need to import your module (that's why we did step 1). Simply type "import YourModuleNameHere" in the top of your test file (where you'll find other imports like "import UIKit", "import XCTest"). Your module name is typically the name of your project.
- Mark all your classes/methods/functions etc that you want to test with public, otherwise they will not be accessible to your test target.
- Start testing.
Note: when you add new classes etc you might have to build before the tests will find the new additions.
Edit: I've made a blog post about this that can be found here: http://pontusarmini.com/xctest-with-cocos2d-and-swift/