Slide 1

Slide 1 text

! ! ! ! ! ! ! ! ! ----------------------------------------- Better UI testing with Swift Robots CocoaConf Next Door | San Jose, CA | June 2017 @designatednerd | spothero.com | justhum.com ----------------------------------------- ! ! ! ! ! ! ! ! !

Slide 2

Slide 2 text

Preamble: UI vs. Unit Tests

Slide 3

Slide 3 text

Unit Tests Testing small pieces of independent code

Slide 4

Slide 4 text

UI Tests Try to use your application like a customer

Slide 5

Slide 5 text

UI Tests Try to use your application like a customer

Slide 6

Slide 6 text

UI Testing Frameworks

Slide 7

Slide 7 text

KIF

Slide 8

Slide 8 text

XCUI

Slide 9

Slide 9 text

Earl Grey

Slide 10

Slide 10 text

So which framework should you use?

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

You can use this technique with any or every UI testing framework

Slide 13

Slide 13 text

! ! ! ! ! ! ! ! ! ------------------------- ROBOTS ------------------------- ! ! ! ! ! ! ! ! !

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Separate the What from the how

Slide 18

Slide 18 text

Separate the What from the how

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

How can I steal adapt this idea for iOS?

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

ಠ_ಠ wtf does that mean?

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Sample Code https://github.com/designatednerd/ GoCubs

Slide 28

Slide 28 text

Sample Code https://github.com/designatednerd/GoCubs

Slide 29

Slide 29 text

Sample Code https://github.com/designatednerd/GoCubs

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

K I F

Slide 32

Slide 32 text

X C U I

Slide 33

Slide 33 text

With DRY refactoring: KIF XCUI

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

With Robots:

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

ROBOTS * - aka controllers

Slide 38

Slide 38 text

More Composable test code

Slide 39

Slide 39 text

How can I steal improve this idea for Swift?

Slide 40

Slide 40 text

Protocols!

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

A brief interlude about #file and #line

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

ANYWAY

Slide 46

Slide 46 text

KIF

Slide 47

Slide 47 text

XCUI

Slide 48

Slide 48 text

Earl Grey

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

KIF XCUI

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Earl Grey

Slide 55

Slide 55 text

Earl Grey

Slide 56

Slide 56 text

How does this technique scale for testing larger apps?

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

Protocol-Oriented Gotchas

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

Subclassing XCTest directly: ✅ class GoCubsXCUITests: XCTestCase { ❌ class GoCubsKIFTests: KIFTestCase { ✅ class GoCubsEarlGreyTests: XCTestCase {

Slide 67

Slide 67 text

MOAR PROTOCOLS!

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

Using the protocol instead: !!!!!!!!!!!!!!!!!!!!!!!!!!!! class GoCubsXCUITests: XCTestCase, RobotTests { class GoCubsKIFTests: KIFTestCase, RobotTests { class GoCubsEarlGreyTests: XCTestCase, RobotTests { !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

Using @testable import:

Slide 74

Slide 74 text

Using @testable import: ❌ class GoCubsXCUITests: XCTestCase, RobotTests { ✅ class GoCubsKIFTests: KIFTestCase, RobotTests { ✅ class GoCubsEarlGreyTests: XCTestCase, RobotTests {

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

Obligatory Summary Slide!

Slide 78

Slide 78 text

Obligatory Summary Slide! Robots will help you: - Separate what what from how

Slide 79

Slide 79 text

Obligatory Summary Slide! Robots will help you: - Separate what what from how - Keep your tests readable

Slide 80

Slide 80 text

Obligatory Summary Slide! Robots will help you: - Separate what what from how - Keep your tests readable - Swap out the underlying implementation

Slide 81

Slide 81 text

Obligatory Summary Slide! Robots will help you: - Separate what what from how - Keep your tests readable - Swap out the underlying implementation FREE ADVICE:

Slide 82

Slide 82 text

Obligatory Summary Slide! Robots will help you: - Separate what what from how - Keep your tests readable - Swap out the underlying implementation FREE ADVICE: - Giving techniques cool names makes it way more likely people will steal use them.

Slide 83

Slide 83 text

Links! — Earl Grey: https://github.com/google/EarlGrey — KIF: https://github.com/kif-framework/KIF — Jake Wharton on Kotlin Robots: https://realm.io/ news/kau-jake-wharton-testing-robots/ — Joe Masilotti on XCTest Helpers: http:// masilotti.com/xctest-helpers/

Slide 84

Slide 84 text

Question Time!

Slide 85

Slide 85 text

Photo Credits! — "Earl Grey" by Ana Ulin https://www.flickr.com/ photos/anaulin/164424396/ — "maybe now I'll get my texts" by frankieleon https:// www.flickr.com/photos/armydre2008/3935008214 — Cubs Winning The World Series, by Matt Slocum, AP https://www.nytimes.com/2016/11/03/sports/ baseball/chicago-cubs-beat-cleveland-indians- world-series-game-7.html