Slide 1

Slide 1 text

Tips and Tricks for Writing Reliable Tests Mani Ramezan @maniramezan

Slide 2

Slide 2 text

About me • My name is Mani, written Mani, pronounced Maani • Tech editor in raywenderlich • Software engineer at Comixology • Former employee of Zocdoc

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Importance level Pyramid of Testing

Slide 8

Slide 8 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 9

Slide 9 text

Challenges • Tightly coupled with app architecture • Test interactions with server • UI Automation tests tends to be flaky • Local ≠ CI environment

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Tips and Tricks

Slide 12

Slide 12 text

Enable Randomize Testing

Slide 13

Slide 13 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 14

Slide 14 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 15

Slide 15 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 16

Slide 16 text

Question What is something that you have found challenging when writing unit tests?

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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")

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

You Can’t Trust a Machine Was asked to type “aetna dc ppo” but it ended up with “aaetna dc ppo” facepalm

Slide 21

Slide 21 text

Always Check Visibility

Slide 22

Slide 22 text

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

Slide 23

Slide 23 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 24

Slide 24 text

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

Slide 25

Slide 25 text

Final question What's one tool or exercise that has helped you to develop a better unit test?

Slide 26

Slide 26 text

Questions? Mani Ramezan [email protected] @maniramezan