Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Writing Reliable Tests in iOS

Writing Reliable Tests in iOS

Avatar for Mani Ramezan

Mani Ramezan

June 10, 2020
Tweet

More Decks by Mani Ramezan

Other Decks in Programming

Transcript

  1. About me • My name is Mani, written Mani, pronounced

    Maani • Tech editor in raywenderlich • Software engineer at Comixology • Former employee of Zocdoc
  2. Agenda • Types of testing • Challenges with writing reliable

    tests • Some tips and tricks • Resources
  3. What kind of tests do you usually include in your

    apps? • https://www.menti.com/tba5bsjyx3 • Go to www.menti.com and use the code 26 94 7
  4. Integration vs. UI Test • Integration Test: Aims to test

    integration between different parts and units of the app • Usually done against mock data • UI Test: Mainly refers to as functional testing, aims to test product integration with other services • Usually done against production code
  5. Challenges • Tightly coupled with app architecture • Test interactions

    with server • UI Automation tests tends to be flaky • Local ≠ CI environment
  6. Challenges cont. • Running all tests could take a long

    time • Managers tend to check for code coverage vs. test coverage • Rely on backend for ability to test in production
  7. Separate AppDelegate • Useful for unit tests • Skip unnecessary

    setups • Customize data store and services • More details: https://marcosantadev.com/fake- appdelegate-unit-testing-swift/
 let isRunningTests = NSClassFromString("XCTestCase") != nil let appDelegateClass = isRunningTests ? NSStringFromClass(TestAppDelegate.self) : NSStringFromClass(AppDelegate.self) UIApplicationMain(CommandLine.argc, CommandLine.unsafeArgv, nil, appDelegateClass)
  8. Data Store • Mainly a big issue for unit tests

    • Need a way to reset data after each unit test • Use in-memory persistent for core data • Be aware of NSUserDefaults
  9. Network Stubbing • Use a third party library to mock

    API responses • OHHTTPStub • Useful for unit test and integration tests • Would need alternatives for production testing
  10. Environment Variables • Can be used to customize tests •

    Test universal links and deeplinks • Feature flags • Camera testing
  11. Reset Warnings and Content • Reset location & Privacy •

    “Reset Warning” on simulator and “Reset Settings” on device • Run before each UITest test case • resetAuthorizationStatus added in Xcode 11.4 • Access Settings app let settings = XCUIApplication(bundleIdentifier: "com.apple.Preferences")
  12. Delete the App • Helps with resetting NSUserDefaults • How?

    https://stackoverflow.com/a/36168101/1450348 • Pros • Clear state • Cons • Increases test runs • Adds to test flakiness
  13. You Can’t Trust a Machine Was asked to type “aetna

    dc ppo” but it ended up with “aaetna dc ppo” facepalm
  14. Keyboard Interaction • Button title: • ”Search” • “Send” •

    “Route" • “Join" • “Go" • "Emergency call” • “Done" • “Continue"
  15. More Tips • Review crash logs • Increase wait interval

    • Always rely on CI results • Constantly monitoring • Re-visiting app architecture • Write tests for new bugs • Code coverage ≠ quality
  16. Resources • iOS Unit Testing and UI Testing Tutorial •

    What's New in Testing (WWDC 2018) • Testing in Xcode • John Sundell • Getting started with Xcode UI testing in Swift • Reduce Flakiness in Swift test • UIKonf 2018 - The Magic of UI Testing • Earlgrey