Slide 1

Slide 1 text

Tips and Tricks for Writing Reliable Tests Mani Ramezan @maniramezan

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

https://media.giphy.com/media/3oKIPnAiaMCws8nOsE/giphy.mp4

Slide 5

Slide 5 text

https://media.giphy.com/media/Px7FQJqhWTGaA/giphy.mp4

Slide 6

Slide 6 text

https://media.giphy.com/media/HCK7W8Qb52ifS/giphy.mp4

Slide 7

Slide 7 text

Agenda • Types of testing • Challenges with writing reliable tests • Some tips and tricks • Resources

Slide 8

Slide 8 text

Importance level

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Challenges • Tightly coupled with app architecture • Test interactions with server • UI Automation tests tends to be flaky • Local ≠ CI environment • Time consuming • Managers tends to check for code coverage • Test in production

Slide 11

Slide 11 text

Enable random testing

Slide 12

Slide 12 text

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)

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Environment Variables • Can be used to customize tests • Test universal links and deeplinks • Feature flags • Camera testing

Slide 16

Slide 16 text

Reset warnings and content • Reset location & Privacy • “Reset Warning” on simulator and “Reset Settings” on device • Run before each UITest test case • Access Settings app let settings = XCUIApplication(bundleIdentifier: "com.apple.Preferences")

Slide 17

Slide 17 text

Delete the app • Helps with resetting NSUserDefaults • Explained https://stackoverflow.com/a/36168101/1450348 • Pros • Clear state • Cons • Increases test runs • Adds to test flakiness

Slide 18

Slide 18 text

You can’t trust a machine

Slide 19

Slide 19 text

Always check visibility

Slide 20

Slide 20 text

Keyboard interaction • Button title: • ”Search” • “Send” • “Route" • “Join" • “Go" • "Emergency call” • “Done" • “Continue"

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Resources • John Sundell • Getting started with Xcode UI testing in Swift • Reduce Flakiness in Swift test • UIKonf 2018 - The Magic of UI Testing • Apple WWDC 2018 - Session 403

Slide 23

Slide 23 text

Questions? Mani Ramezan [email protected] @maniramezan