$30 off During Our Annual Pro Sale. View Details »

Better UI Testing with Swift Robots - CocoaConf Next Door San Jose, June 2017

Better UI Testing with Swift Robots - CocoaConf Next Door San Jose, June 2017

My deck on using the Robot pattern for more readable, more flexible tests in Swift.

Codez: https://github.com/designatednerd/GoCubs

Ellen Shapiro
PRO

June 06, 2017
Tweet

More Decks by Ellen Shapiro

Other Decks in Technology

Transcript

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

    View Slide

  2. Preamble:
    UI vs. Unit Tests

    View Slide

  3. Unit Tests
    Testing small pieces of independent code

    View Slide

  4. UI Tests
    Try to use your application like a customer

    View Slide

  5. UI Tests
    Try to use your application like a customer

    View Slide

  6. UI Testing
    Frameworks

    View Slide

  7. KIF

    View Slide

  8. XCUI

    View Slide

  9. Earl Grey

    View Slide

  10. So which framework
    should you use?

    View Slide

  11. View Slide

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

    View Slide

  13. ! ! ! ! ! ! ! ! !
    -------------------------
    ROBOTS
    -------------------------
    ! ! ! ! ! ! ! ! !

    View Slide

  14. View Slide

  15. View Slide

  16. View Slide

  17. Separate the
    What
    from the
    how

    View Slide

  18. Separate the
    What
    from the
    how

    View Slide

  19. View Slide

  20. View Slide

  21. How can I steal adapt this idea
    for iOS?

    View Slide

  22. View Slide

  23. ಠ_ಠ
    wtf does that mean?

    View Slide

  24. View Slide

  25. View Slide

  26. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  30. View Slide

  31. K
    I
    F

    View Slide

  32. X
    C
    U
    I

    View Slide

  33. With DRY refactoring:
    KIF XCUI

    View Slide

  34. View Slide

  35. With Robots:

    View Slide

  36. View Slide

  37. ROBOTS
    * - aka controllers

    View Slide

  38. More Composable
    test code

    View Slide

  39. How can I steal improve this idea
    for Swift?

    View Slide

  40. Protocols!

    View Slide

  41. View Slide

  42. A brief interlude
    about #file and #line

    View Slide

  43. View Slide

  44. View Slide

  45. ANYWAY

    View Slide

  46. KIF

    View Slide

  47. XCUI

    View Slide

  48. Earl Grey

    View Slide

  49. View Slide

  50. KIF
    XCUI

    View Slide

  51. View Slide

  52. View Slide

  53. View Slide

  54. Earl Grey

    View Slide

  55. Earl Grey

    View Slide

  56. How does this technique
    scale
    for testing larger apps?

    View Slide

  57. View Slide

  58. View Slide

  59. View Slide

  60. View Slide

  61. View Slide

  62. View Slide

  63. View Slide

  64. Protocol-Oriented
    Gotchas

    View Slide

  65. View Slide

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

    View Slide

  67. MOAR PROTOCOLS!

    View Slide

  68. View Slide

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

    View Slide

  70. View Slide

  71. View Slide

  72. View Slide

  73. Using @testable import:

    View Slide

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

    View Slide

  75. View Slide

  76. View Slide

  77. Obligatory Summary Slide!

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  82. 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.

    View Slide

  83. 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/

    View Slide

  84. Question Time!

    View Slide

  85. 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

    View Slide