Turbo-Boost Your Unit Tests in Xcode and Swift
Unit tests are just as important as your normal programming code. Getting them right is important. Doing a good job is important. Get this right, do.
Difficulty: Easy | Normal | Challenging
- Some basic understanding of functions and methods in Swift.
- Some String interpolation comes up later, but you can skip that bit.
- Some experience of setting up classes in Swift would be beneficial.
Unit testing: Software testing where individual units of the software are tested.
Before we start
You should be writing with a test-driven mindset. This means you should think about writing tests first. Although this sounds like it might slow you down, there is actually reams of evidence that writing tests first not only makes you a better programmer but a faster one too. An alternative to full test driven development is test-driven bug fixing where you use a similar technique to solve bugs within your code. As ever, the best person to decide what strategy to use for your code is you.
You should also think about whether code coverage of 100% is achievable, or even a worthy goal. Many organisations talk of an 80% test target, but experienced programmers may tell you even that is unrealistically high. Experience tells us all, and helps us decide when to push back on targets like the ones mentioned here.
Gather data within your tests
You might be told (by an employer, or a mentor) to try to acheive 80 or 100% test coverage. Now how are you supposed to measure this?
You need to select as
Edit Scheme... from your target
Test on the left-hand side, and select the tick box to
Gather coverage for all targets
You should be aware that collecting code coverage data like this takes more time. Your tests…