Skip to content

Stop wasting time and use this smart Swift test template

This seems to be a recurring theme in my recent posts but I think it’s an important one: don’t settle for writing boiler plate code.

My focus this time is eliminating the boiler plate when creating a new test file by using a smart Swift test template so you can start writing your test straight away.

So what is this boiler plate code I’m talking about?

@testable import

Not a big deal but reaching for that @ is annoying – we’re not writing Objective-C anymore!

setUp()

The file template might generate the setUp method for you (although perhaps with a useless comment to delete) but you do have to create an instance variable and then initialise it in the setUp method.

tearDown()

Again, it’s probably generated for you but likely another useless comment for you to delete and then you must set the instance variable you just created to nil.

Note

Setting your properties to nil is a necessary evil. But luckily, this is something else we can automate 🎉.

Test method declaration

Then you get to write your test method. And, if you organise your tests like me, you might want to write a MARK: comment indicating the method tested in this section of the file. You might also have a specific format that your test methods names follow. Oh, and you’ll want to write a descriptive name for your test.

The test

After all this, you can finally focus on what actually matters; your test.

All of these steps every time you create a new test class? Not on my watch!

There are some things you can do in Xcode like creating code snippets and custom file templates to speed up this process but AppCode’s file templates are really quite special.

Swift test template example

Let’s take a look at writing a new test class for my Bowling game.

Generate test file from Swift test template

See how little code I had to write? And how I started writing the test almost straight away?

Let’s take a closer look.

@testable import

That pesky syntax has been written for me and I just have to fill in the module I’d like to import. You can of course hardcode this module if you only work on one project.

setUp()

The set up method is written for you – great. But the main focus here is the bowling instance variable. The template knows that we’re testing the Bowling type because my test file is called BowlingTests. Not only that; the template will smartly name the instance variable too.

Note

If your class name is long then the instance variable is named from the last word of the class. So ReallyLongNameManager is named manager instead of reallyLongNameManager. You’re welcome 😎

tearDown()

Now we have an instance variable, it is set to nil for us automatically.

Test method declaration

Most of this is generated for us but notice I only had to type the method name once which filled in my MARK: comment, test method name, and the invocation of that method in my test.

The test

And finally, the cursor is placed at the end of a generated ​​​XCTAssert for me to fill in. Don’t you love it when a plan comes together?

Extra credit

If you are following TDD (and you should be following TDD) then you are conveniently ready to create your new class and method from usage.

Gimme the Swift test template!

Lucky you, I’ve done all the hard work for you. Download this file and copy it to ~/Library/Preferences/<your-appcode-version>/fileTemplates

Then go to Preferences -> Editor -> File and Code Templates, find the Swift Test template, and check Enable Live Templates.

Enable live templates screenshot

Get in touch if you’re interested in writing your own smart Swift File Templates and want to know more and I’ll get writing another post.

Subscribe now to hear more about AppCode tips and tricks

Published inAppCodeEssential tips you didn't know that will make you ditch XcodeiOS

Be First to Comment

Leave a Reply