The Swift Unit Testing Template You’re Missing

March 22, 2016 — 2 Comments

Back in Xcode 4 days, Apple’s file template for unit tests was awkward and bloated. So I made my own.

Fast-forward 5 years to Xcode 7.3. I’m starting to write Swift. The first thing I want is unit tests — let’s create a new test suite. I look in Xcode’s file template library. Where’s the Swift unit testing template?

Where is it? Apple didn’t provide one?

Fine. I made my own.

Swift unit testing template

Edit: ⌘N for New File does let you choose the language. But not when you drag it in from the Utilities panel! In any case, the resulting file is stuffed with cruft which wastes your time. My template fixes that.

What’s in Apple’s template (when there is one)?

What’s in a good unit testing template? When I create a new test suite from a template, what do I want?

I don’t want to have to delete anything.

Apple is fond of filling their templates with tutorial cruft. In their Objective-C unit testing template, they have:

  • A file comment block
  • An empty setUp
  • An empty tearDown
  • An empty test case
  • An empty performance test

I say “empty” but every empty method includes explanatory comments!

The first thing I do when I use Apple’s templates is delete everything. I don’t need a tutorial, I need the least code that works.

Apple figured out in Xcode 5 that an Objective-C test suite doesn’t need a header file. Unfortunately, their template is still stuffed with cruft. That’s why I continue to use my own test template for Objective-C.

There is one thing I want: a single failing test. That helps me ensure that I’ve set up the test suite correctly. Once I verify the expected failure, I delete the test. The empty suite is then ready for new tests.

Swift unit testing template? Use this instead

Why hasn’t Apple included a template for Swift unit testing? I have no idea. No matter, I made my own.

Edit: Helpful readers have pointed out that the template is there for Swift. ⌘N for New File prompts you for language. Xcode doesn’t bother asking if you drag it in from the File Template Library, though.

Someday, they will include a template. When that day comes, what will it contain? Judging from their Objective-C template, it’ll have cruft. You’ll either spend time deleting it, or you’ll leave it in place, bogging down your codebase.

Let’s move faster, and keep our code lean! Download my Swift unit testing template. (Objective-C template also included.)

You’ll also want Swift code snippets for writing unit tests: new test, setUp, tearDown, outlets and actions, etc. Get those code snippets here.

Jon Reid

Posts Twitter Facebook Google+

I've been practicing Test Driven Development (TDD) since 2001. Learn more on my About page.

2 responses to The Swift Unit Testing Template You’re Missing

  1. Great writeup, Jon!

    I’ve been creating my own file templates ever since I learned about them from your wonderful Objective-C example. :]

    One suggestion for improving the Swift template would be to make the template icon a different color than the Objective-C version. I went with orange to feel more Swifty, and it really makes a big difference in quickly identifying it from the other templates.

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>