Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I setup a Unit Test for Swift?

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

like image 969
Frederick C. Lee Avatar asked Sep 30 '14 18:09

Frederick C. Lee


1 Answers

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.

like image 180
Pasan Premaratne Avatar answered Sep 23 '22 19:09

Pasan Premaratne