Back in Xcode 4 days, Apple’s file template for Objective-C 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.
Why am I not surprised that Apple’s template for Swift unit tests is filled with cruft?
Fine. I made my own.
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:
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.
The Swift template doesn’t have a header file, of course. But it’s still filled with the same cruft. And you know what happens to 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)
As of 05 April 2017, the download fixes a problem which kept my templates from working in AppCode.
You’ll also want Swift code snippets for writing unit tests: new test, setUp, tearDown, outlets and actions, etc. Get those code snippets here.
Programming was fun when I was a kid. But working in Silicon Valley, I saw poor code lead to fear, with real human costs. Looking for ways to make my life better, I learned about Design Patterns, Refactoring, and Test-Driven Development (TDD). Programming became fun again! I've now been doing TDD in Apple environments for 17 years. I'm committed to software crafting as a discipline, hoping we can all reach greater effectiveness and joy.
Please log in again. The login page will open in a new window. After logging in you can close it and return to this page.