Adding class files to Test project


#1

In the chapter on unit testing, it states that in order to test a class, you need to “import RanchForest” and you need to make all the elements you are testing public to provide visibility to the test target.

I noticed that by simply adding the classes to be tested to the RanchForecastTests target (by using the target membership section in the file inspector), I didn’t have to do either of those two steps above. Is there any disadvantage of doing this? It certainly seems a lot easier than having to change the visibility of my classes.


#2

This approach works fine for small projects, but it ends up becoming fairly cumbersome to keep the test target membership in sync with the main app (or framework) target as your project grows and you add more and more types and files. At this point in time we think it is best to mark things public and import the module, although we are hoping that Apple comes up with a smoother way to do this.


#3

Now with Swift 4.1 and Xcode 9.4.1 you don’t need to make anything public. You can import this way:

@testable import RanchForecast

And everything in the RanchForcast module is avail for testing.