In this video, Mark DiFranco explores the importance of adding unit tests to your iOS projects. He touches on some common reasons that unit tests are not written, and why these patterns should be avoided.
- [Instructor] We've all been there before, the project team wants to add more features to the app, but as the app continues to grow in size, each feature gets harder and trickier to implement. More bugs start popping up, and the dev team ends up spending more time on fixing bugs rather than implementing new features. Trying to write tests is a huge pain and takes a lot of time and effort, which need to be spend fixing bugs and adding features. So the test never get written and the cycle continues. It's easy to think of tests as a nice-to-have, but really they should be a required part of every project.
It may save you time in the short term to skip writing tests but it definitely hurts in the long term. Writing tests forces you to consider all the cases of your code, making the code you write stronger. They're a good form of documentation since they map up the expected behavior of your app, and they're priceless tools to have at your disposal when refactoring. A common excuse for not writing tests is that they're too difficult to write. Depending on how you organize your code, it is sometimes not possible to control all the inputs and outputs of a class. Singletons are notoriously difficult to test.
Part of writing test is ensuring the state of the class you're testing is reset before each test. It's easy to achieve this by creating new instances before each test but that can't be done with singletons. Any class that references a singleton will also be hard to test, since you won't be able to isolate that class from the singleton. Another common architectural anti-pattern is to put all your logic into your view controllers. This not only makes it difficult to refactor in the future, but testing UI code is cumbersome. Today we're going to look at a method of architecting your app to not only make your code easy to test, but also easier to read and understand.
- Why write unit tests?
- What is dependency injection?
- Using protocols to help with tests
- Handling external dependencies
- Anatomy of a test case
- Writing tests
- Analyzing code coverage
- Visualizing test results
- Writing and extending UI tests