Unit Testing Your Swift App

Riccardo Cipolleschi
Mac O’Clock
Published in
5 min readApr 15, 2020

--

Hi everybody, I’m Riccardo. Senior iOS Engineer at Bending Spoons, I breathe iOS development, both apps and tools and I love to share my knowledge with others.

In the past few days, I have been writing a lot of unit tests for a huge library we are writing at Bending Spoons. Even if it can sound not really exciting, I like it. Writing tests has several pros that are often underestimated:

  1. It is extremely useful to verify that the code you are writing is doing what you expect, without building and launching an app.
  2. It protects you from breaking other code when you fix a bug or add a feature: running all the tests after a change will prevent regression issues on other parts of the codebase.
  3. It helps you creating smaller PR: if you are not used to writing tests, the tendency is to write the whole feature before pushing it, instead of breaking it in smaller parts. Bigger PRs are really hard to be reviewed properly and that is not good. Adding the unit tests lets you define precisely the boundaries of your components and the resulting PR will be smaller and easier to read and review. An added benefit: the code PR and the test PR can be two different, smaller PRs
  4. It helps a lot in structuring the code. The code has to follow some principles about how it is structured to be easily testable.

--

--

Riccardo Cipolleschi
Mac O’Clock

Hey there, I’m Riccardo. Software engineer at Meta. I have a passion for iOS and I love to share my knowledge with others.