Why test your JSON parser?

One of the big benefits to creating a separate JSON parser class is that we can write automated tests for it easily. But why would you want to write tests? What benefit could they possibly provide?

Unit tests provide at least five main benefits: they help you ensure correctness during development, catch bugs in existing features, allow you to refactor with the confidence that you didn’t break anything, run much much faster than manual tests, and they don’t even require you to be present while they run. Let’s take a look at each of these individually.

1. Unit tests ensure correctness during development

As you’re developing code, good unit tests can give you confidence that the code you just wrote does what you think it does. And if when your code isn’t right, unit tests will quickly point that out. Catching bugs early like this is the best time to catch them, since tracking down and fixing them is always easiest right after you wrote the code. It’s still fresh in your mind; you can quickly get back into it and fix whatever’s broken.

If you develop a small piece of code, then run a test that fails, you’ll be sure to find the issue quickly. On the contrary, if you develop a small piece of code, ship it to production, and get a bug report from a user, it’ll take you much longer to track down and fix that piece of code.

2. Unit tests catch bugs in existing features

When you have good unit tests that are run frequently, you can catch regressions before they get to the QA team – or worse, to your users. How?, you wonder. Well, once you’ve written a test that verifies some behavior, that test continues to verify that behavior every time the test suite is run. As long as you have good tests that are run frequently, whenever you break something in your code, you’ll have a failing test as soon as the test suite runs again.

Establish the habit of running your tests before every commit, and get a continuous integration server set up to run the test suite whenever code is pushed to your central repository. With these habits and processes, you can be sure your tests will run frequently, protecting your code against regressions and catching them as soon as possible.

3. Unit tests allow you to refactor with confidence

Over time, most code needs to be refactored. This is true even if you made excellent decisions early on; requirements grow and change, and at some point you’ll have code that’s doing something it wasn’t originally intended to do. It becomes hard to maintain and hard to add new features.

Refactoring is part of life as a developer, and it can be downright scary if you don’t have good unit tests in place. Without them, it’s really hard to know whether your refactoring broke anything in the process. Sure, you can always test manually after you finish refactoring, but unit tests can tell you right away when something breaks – without you having to launch and navigate through the app. They can pinpoint the exact method that failed to produce the expected output, making it faster and easier to fix the problem. Plus…

4. Unit tests run faster than manual tests

A unit test can run in a fraction of a second. It’s impossible to run a manual test in that time. Even a whole suite of unit tests can usually run in less than a minute – which is probably about as long as it takes to run one or two manual tests. And on top of that…

5. Unit tests run without you needing to be present

Once you’ve written unit tests, they can be executed again and again without you being there. You can run them while you sip your coffee or have them run automatically on a continuous integration server every time you push your code. Your time is valuable; computer time is cheap. If you can write a test to verify behavior instead of having to test it manually over and over – every time you make a change – the automated test is worth its weight in gold.

But even with all these benefits, is it worth spending the time to write tests? You still have to run manual tests, so if you write unit tests, aren’t you wasting time you could be doing that or writing production code?

I’d argue that you actually save time by writing tests. By reducing the number of bugs in your code, you’re spending less time doing manual testing and less time tracking down problems. And you’re giving yourself an extra level of confidence you just can’t get without them – even if you run thousands of manual tests.

Unit testing is a big part of my process as a developer, and I fully believe it makes the apps I develop more stable, less buggy, and easier to maintain. Whenever I start a new app, whether it’s for a client or for myself, I always write unit tests. And even if I join an existing project that doesn’t have unit tests, I often add some to ensure that the code I’m writing does what I want it to.

So, how can you write unit tests for your shiny new JSON parser?

This article is an excerpt from Parsing JSON in Swift. Learn how to write unit tests for your JSON parser, how to transform dictionaries into model objects, and the dangers of forced type casting with the full book.