For a long time, I've programmed without writing a single line of test. Actually, I thought that writing tests were a waste of time until I start working on a company that my job was to create new features to a poorly coded app. Every time I coded a new line I revised the app a hundred of times just to make sure it is still working. That made me realize that testing is very important but even with that in mind I didn’t give the right value to it.
One day my coworker came with a great idea. Let’s start this new project with a new process, let’s use TDD he said. But what the hell is TDD?
TDD is a software process that encourages programmers to write tests before writing code.
Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: requirements are turned into very specific test cases, then the software is improved to pass the new tests, only.
Until that moment I haven’t written a lot of test code, I knew that this TDD decision would make me suffer in a good way. I would learn about testing, new libs, and the most important: How to write good test code.
For two weeks we did pair programming so I could get used to TDD and it’s lifecycle, which is: You write a test code and run. It probably will fail, actually, it should fail and now you can write some code but just the minimum to make your test pass. Run your test again, and it should pass. Now you can write more test or refactor your previous code without adding new features.
Within this challenge we decided to use a few libraries to help us write tests in iOS: Quick, Nimble, Nimble+Snapshots and KIF:
GitHub - Quick/Quick: The Swift (and Objective-C) testing framework.
Quick - The Swift (and Objective-C) testing framework.
Helps you writing tests, defining a context for different kinds of test,
beforeEach, and the test itself. It is useful to make your tests more readable.
GitHub - Quick/Nimble: A Matcher Framework for Swift and Objective-C
Nimble - A Matcher Framework for Swift and Objective-C
Makes your assertions more readable too, instead of using
XCTAssertEqual(1+1,2,"expected one plus one equal two") you write
expect(1+1) == 2 . So much easier.
GitHub - ashfurrow/Nimble-Snapshots: Nimble matchers for FBSnapshotTestCase
Nimble-Snapshots - Nimble matchers for FBSnapshotTestCase
Is a great lib that helps a lot when we talk about UI unit testing. It takes a screenshot of your UI and check if you don’t mess it up. You can record a snapshot using
expect(view) == recordSnapshot("screenshot-name") and after that you can check it by changing
expect(view) == snapshot("screenshot-name")
Is a framework that we used to test our UI events like the tap on a button or a scroll behavior on an UITableView.
With all this testing and libraries what the hell I learned with TDD?
First of all, I learned how to write tests, from a JSON’s parse to a UIView and this is amazing because I don’t need to test my app a billion times in the simulator (or device) before I release a new feature. Our unit tests do this for us.
Second, I get used to writing tests. It is not a monster under the bed anymore, actually, now I prefer to write tests before code. It makes writing better code because if you have troubles writing tests probably your code is a mess and you should refactor it.
This post is just an introduction of a series of post that I’m working on to show how to write test using Quick, Nimble, Snapshot and KIF from JSON’s parse to ViewControllers including protocols, table views and more.
If you have any suggestion or wonder how to write certain kind of tests, let me know that I’ll try to do it on a post.