5

I feel it prudent to attempt to master Unit Testing/Swift.
I viewed WWDC's Objective-C version and pretty much understand the Objective-C paradigm: importing the headers that the Unit Test depends on, etc.

The 'fetchFlickrPhotoWithSearch()' is unknown to the Unit Test. So...

Being that a Unit Test module/target is outside the scope of the application target, I assume that I need to import the particular Swift file (similar to Objective-C's header paradigm) that has the functions I wish to test.

But the compiler flags this import as 'No such module...'

So... how do I make my Swift APIs available to the unit test?

enter image description here

Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105

2 Answers2

8

The solution recommended by Steve Rosenberg is an easy one but leads to issues that are hard to debug (an instance of which is documented here if you're interested).

The recommended approach is to edit your app target's (not the test target) Build Settings and set "Defines Module" under Packaging to YES.

With that done, in your test target, at the top of each .swift file, import your projects main module to get access to your classes, structs, etc. The name of the main module is typically the same name as your Xcode project file.

Note: When following this approach, you will have to annotate all class/struct/enum methods that you want to test with the public access control modifier.

Pasan Premaratne
  • 537
  • 2
  • 9
  • 33
0

There is a good tut here:

http://natashatherobot.com/swift-unit-testing-tips-and-tricks/

Making sure the class file you want to test has it's target membership checked for Tests in the file Identity inspector in the utilities panel. This caught me the first time.

Steve Rosenberg
  • 19,348
  • 7
  • 46
  • 53
  • I noticed that by default (or happenstance) the desired .swift file was also included in the test target. What was MISSING was the PUBLIC qualifier for the particular function I wish to test. I don't need the '#import...' statement. *It works!* – Frederick C. Lee Sep 30 '14 at 19:54