Slide 1

Slide 1 text

Testing your Swift Code M AT T N E D R I C H

Slide 2

Slide 2 text

ABOUT ME AT O M I C O B J E C T M AT T N E D R I C H @ M AT T N E D R I C H M AT T. N E D R I C H @ AT O M I C O B J E C T. C O M

Slide 3

Slide 3 text

GOAL Introduce basic testing approaches in Swift Unit Tests UI Tests Discuss some useful libraries Continuous Integration & Automatic Deployment

Slide 4

Slide 4 text

Changing Landscape [Objective C]

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

What’s the deal with Swift? ¯\_(ϑ)_/¯

Slide 10

Slide 10 text

WHATS THE DEAL WITH SWIFT Limited reflection support Required by most mocking approaches Different runtime rules Can still write lots of great tests though

Slide 11

Slide 11 text

XCTest

Slide 12

Slide 12 text

https://github.com/mattnedrich/codemash-testing-demo

Slide 13

Slide 13 text

XCTEST EXAMPLE

Slide 14

Slide 14 text

XCTEST ASSERTIONS XCTAssertEqual(expression1, expression2, format…) XCTAssertNil(expression, format…) XCTAssertNotNil(expression, format...) XCTAssertTrue(expression, format…) XCTAssertThrows(expression, format…) XCTAssertNoThrow(expression, format...)

Slide 15

Slide 15 text

XCTEST SYNCHRONOUS EXAMPLE

Slide 16

Slide 16 text

XCTEST SYNCHRONOUS EXAMPLE

Slide 17

Slide 17 text

XCTEST ASYNCHRONOUS EXAMPLE

Slide 18

Slide 18 text

XCTEST ASYNCHRONOUS EXAMPLE

Slide 19

Slide 19 text

UIViewController

Slide 20

Slide 20 text

UIVIEWCONTROLLER Test outlets Test some simple UI behavior

Slide 21

Slide 21 text

UIVIEWCONTROLLER

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

UITABLECONTROLLER

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

XCTEST SUMMARY Bare Bones, lots of boilerplate Poor test readability Can find ways to test most scenarios

Slide 26

Slide 26 text

Testing Libraries

Slide 27

Slide 27 text

QUICK AND NIMBLE

Slide 28

Slide 28 text

QUICK AND NIMBLE

Slide 29

Slide 29 text

QUICK AND NIMBLE

Slide 30

Slide 30 text

QUICK AND NIMBLE XCTEST

Slide 31

Slide 31 text

PREVIOUS ASYNC EXAMPLE

Slide 32

Slide 32 text

QUICK AND NIMBLE - ASYNC

Slide 33

Slide 33 text

Mocking

Slide 34

Slide 34 text

H T T P : / / B L O G . P R A G M AT I C E N G I N E E R . C O M / S W I F T - T H E - O N LY - M O D E R N - L A N G U A G E - W I T H - N O - M O C K I N G - F R A M E W O R K /

Slide 35

Slide 35 text

SWIFT TEST MOCKING Traditional mocking support does not exist Can create sub classes and override functions Must loosely couple things and rely on protocols for everything This can be great It can also lead to a large amount of unnecessary abstraction

Slide 36

Slide 36 text

MOCKING LIBRARIES Some libraries do exist Usually create types ahead of time in batch Anyone have experience with them?

Slide 37

Slide 37 text

UI Testing

Slide 38

Slide 38 text

UI TESTING Allow tests to perform UI interactions and assert behavior Similar to KIF tests Can work nicely as end-to-end or integration tests

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

UI TESTING

Slide 41

Slide 41 text

UI TESTING

Slide 42

Slide 42 text

UI TESTING

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

UI TESTING - PASS DATA TO TEST Use the launch arguments

Slide 45

Slide 45 text

UI Testing - Recording

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

Continuous Integration

Slide 48

Slide 48 text

LOTS OF CI OPTIONS Circle CI Travis CI TeamCity …

Slide 49

Slide 49 text

Circle CI

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

+

Slide 53

Slide 53 text

STEP 1: PUT YOUR PROJECT ON GITHUB

Slide 54

Slide 54 text

STEP 2: SHARE YOUR XCODE SCHEME

Slide 55

Slide 55 text

STEP 3: PUSH TO GITHUB, LOG INTO CIRCLE CI

Slide 56

Slide 56 text

STEP 4: SELECT YOUR PROJECT AND REPOSITORY

Slide 57

Slide 57 text

STEP 5: CIRCLE CI WILL BUILD YOUR PROJECT

Slide 58

Slide 58 text

STEP 5: FAILED BUILD

Slide 59

Slide 59 text

STEP 6: ADD CIRCLE.YML CONFIGURATION FILE

Slide 60

Slide 60 text

STEP 7: RE-RUN WITH XCODE 8.2

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

CIRCLE CI IS NOW WORKING Your project will be built and tests will be run every time someone pushes to master Everyone subscribed to the Github repository will receive email notifications

Slide 63

Slide 63 text

What About Deployment?

Slide 64

Slide 64 text

GOAL Every time a change is pushed to master Build project Run tests If tests pass, push build to TestFlight or HockeyApp, etc. Release build to test users with the press of a button

Slide 65

Slide 65 text

STEP 1: INSTALL FASTLANE

Slide 66

Slide 66 text

STEP 1: INSTALL FASTLANE

Slide 67

Slide 67 text

STEP 2: CREATE CERTIFICATES

Slide 68

Slide 68 text

STEP 2: CREATE CERTIFICATES fastlane cert --output_path certificates This will create three files something.cer something.certSigningRequest something.p12

Slide 69

Slide 69 text

STEP 2: CREATE CERTIFICATES

Slide 70

Slide 70 text

STEP 3: CREATE PROVISIONING PROFILE

Slide 71

Slide 71 text

STEP 3: CREATE PROVISIONING PROFILE fastlane sigh This will create something like AppStore_com.mattnedrich.codemash-testing-demo.mobileprovision

Slide 72

Slide 72 text

STEP 3: CREATE PROVISIONING PROFILE

Slide 73

Slide 73 text

STEP 4: USE GYM TO DISTRIBUTE YOUR APP

Slide 74

Slide 74 text

STEP 4: USE GYM TO DISTRIBUTE YOUR APP

Slide 75

Slide 75 text

STEP 4: USE GYM TO DISTRIBUTE YOUR APP