WWDC Notes

Testing Tips & Tricks

Show Apple's description.
Testing is an essential tool to consistently verify your code works correctly, but often your code has dependencies that are out of your control. Discover techniques for making hard-to-test code testable on Apple platforms using XCTest. Learn a variety of tips for writing higher-quality tests that run fast and require less maintenance.
  • The usual reminder about the “Pyramid of Tests”, three kinds of tests (from small to big, fast to slow):
    • Unit: test specific functions/methods
    • Integration: test classes/methods interaction
    • End-To-End (System Tests, UI): test the user final experience
  • NotificationCenter can use different environments (kind of like userdefaults have different groups): you should avoid using the default notification center in your tests as this may affect other parts of the code (the test is therefore NOT isolated). To make things testable you should inject the notificationCenter in all your methods, going from this: To this:
  • You can also add a default value so you need to change the parameter only in the tests:
  • When creating mocks, beside relying as much as possible on protocols, another neat thing your mock classes/struct should have is the possibility to overwrite some of their methods, so every test can change the behaviour of the mock accordingly on what you’re trying to test
  • To make tests run even faster, you can try to skip some setups in your app launch by adding an environment variable or a launch argument in your schema: And then use it in your app:

Missing anything? Corrections? Contributions are welcome 😃

Related

Written by

zntfdr

Federico Zanetello

Federico Zanetello is an iOS Engineer with strong passion for Swift, minimalism, and design. When he’s not busy automating things, he can be found writing at fivestars.blog and/or playing with the latest shiny toys.