One of the biggest challenges to Unit Testing in Swift was the initial setup. Before Swift 2, you either had to make everything public or remember to add all your files to the testing target (more on this in my blog post on testing in Swift here). But as of yesterday, this annoying testing issue has been elegantly solved!

All you need to do is:

1. Create Your Internal Class

I just created a super simple internal Model object in my brand new TestingTests project with one function:

Again, notice that I’m not adding this model to my test target and not making the class or my method public!

2. Import with @testable

In your test target, just import the module you want to test using the @testable keyword:

3. Test Away!

That’s it, now you can use all the internals of your class for testing purposes 🙂

Thank you Swift team!

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.