Quality Coding
Shares

Automatically Generate Swift tearDown with This AppCode Plugin

Shares

Those of you who are regular visitors to qualitycoding.org may have already read Jon’s article outlining the life cycle of XCTestCase objects during the execution of a test suite. If you haven’t, or you want to refresh your memory, take a moment to read the original article.

Jon discusses the importance of the setUp and tearDown methods. And, more specifically, why we should be deallocating our properties in the tearDown method.

Writing a tear down method is one of those repetitive tasks. It follows a specific pattern: all properties should be set to nil. It doesn’t really require any brain power. It’s just one of those annoying tasks that you have to remember to do when writing a test.

It’s tasks like this that should be automated. And now with the Swift tear down inspection AppCode plugin, we can.

What is an Inspection?

Inspections are algorithms that analyse code for potential problems. They appear as warnings in the editor and often provide an option to fix that problem for you. An example we’re all familiar with is when we define a local variable that is not mutated. AppCode (and Xcode) will provide a warning and offer to change it to a constant.

Example of AppCode inspection

The Swift tear down inspection AppCode plugin

You guessed it, I’ve made an inspection AppCode plugin to inspect the tear down methods within a Swift XCTestCase. As you would expect, the inspection examines test files for properties who are not set to nil in the tear down method and provides a warning if any are found. Then it will provide a quick fix to either generate the tear down method or, if it already exists, ensure all properties are set to nil. Check it out.

Swift tear down method inspection AppCode plugin

But it gets more interesting.

Code cleanup

AppCode has this feature called Code Cleanup. It enables you to run an inspection on a set of files and then automatically apply a quick fix to each problem.

Imagine your team decides to start writing tear down methods for its tests. You have hundreds of tests files; most of which have unmaintained tear down methods or none at all. You could go to each file and fix the tests one by one. Or, you could use Code Cleanup to fix all the problems in one action.

Once you have installed the tear down inspection navigate to Code -> Code Cleanup, choose the scope and click OK. Then sit back and watch AppCode fix all those tear down methods!

Swift tear down method inspection AppCode plugin with Code Cleanup

Told you it would get more interesting. But there’s even more.

Cleanup on commit

It’s easy to forget to write or update your tear down methods. And for good reason; when writing your best code you shouldn’t have to focus on the little maintenance chores like this.

AppCode has its own built-in VCS interface so you can commit, push, pull, etc, right inside the IDE. So what? Well, as part of its commit interface, you can ask AppCode to perform Code Cleanup on all your changes. This means anytime you add or modify a test file, AppCode will ensure the tear down method is up to date – free tear down method maintenance 🎉

It couldn’t be easier: navigate to VCS -> Commit Changes (⌘⌥C), check the Cleanup box, and click Commit. Sit back, relax, and watch AppCode update any of those unmaintained tear down methods before committing them along with your changes. Swift tear down method inspection AppCode plugin in commit code cleanup

Gimme the AppCode plugin!

Don’t all rush at once! There are enough to go around. It’s pretty easy to install a plugin in AppCode but you can find installation and usage instructions for my AppCode plugin on GitHub.

Extra tip

If, like me, you don’t like hopping between the keyboard and the mouse, you can efficiently hop to the next warning or error using ⌘” and the previous using ⌘⇧”. Watch me efficiently fix both of these warnings by hopping between them without touching the mouse.

Hop the cursor from one warning to the next

What else can these plugins do?

Well, the short answer is they can help us out a lot. They can make our code more clean and consistent, they can help us stop writing repetitive boiler plate code, they can warn us of potential bugs or inefficiencies in our code, and so much more. Take writing mocks, for example. It’s such a repetitive task! I got sick of writing the same boiler plate code everywhere so I made an AppCode plugin to do it for me. Swift is such a young language and the tools still evolving. But this does mean that there are lots of opportunities for improving our developer tools. So if you have an idea, then you can make an AppCode plugin; and I have just the article to get you started. Happy coding!

About the Author Sean Henry

Sean is a coder, primarily focused on writing great Swift apps. He is travelling the world, blogging about how to craft quality code and master the tools. Read more about Sean on his blog.

follow me on:

Leave a Comment: