Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Testing

Swift Testing

Tutorial which describes how to add unit tests and BDD to your Swift application. Also provides an introduction to CocaoPods.

Associated code is stored on GitHub https://github.com/sconaty/SwiftTestingTutorial

Sheldon Conaty

November 12, 2014
Tweet

Other Decks in Programming

Transcript

  1. - http://cocoapods.org “CocoaPods is the dependency manager for Objective-C projects.

    It has thousands of libraries and can help you scale your projects elegantly.” Not yet fully working for Swift: https://github.com/CocoaPods/CocoaPods/issues/2272 https://github.com/CocoaPods/CocoaPods/pull/2222
  2. CocoaPod Installation • Assuming gem is installed… ! • If

    you need to install gem then… • Download from https://rubygems.org/pages/ download • Requires git version 1.7.5 or newer See http://guides.cocoapods.org/using/getting-started.html#getting-started
  3. $ sudo gem install cocoapods Password: Fetching: i18n-0.6.11.gem (100%) Successfully

    installed i18n-0.6.11 Fetching: multi_json-1.10.1.gem (100%) Successfully installed multi_json-1.10.1 Fetching: activesupport-3.2.20.gem (100%) Successfully installed activesupport-3.2.20 Fetching: nap-0.8.0.gem (100%) Successfully installed nap-0.8.0 Fetching: json_pure-1.8.1.gem (100%) Successfully installed json_pure-1.8.1 Fetching: fuzzy_match-2.0.4.gem (100%) Successfully installed fuzzy_match-2.0.4 Fetching: cocoapods-core-0.34.4.gem (100%) Successfully installed cocoapods-core-0.34.4 Fetching: claide-0.7.0.gem (100%) Successfully installed claide-0.7.0 Fetching: colored-1.2.gem (100%) Successfully installed colored-1.2 Fetching: xcodeproj-0.19.4.gem (100%) Successfully installed xcodeproj-0.19.4 Fetching: cocoapods-downloader-0.7.2.gem (100%) Successfully installed cocoapods-downloader-0.7.2 Fetching: cocoapods-plugins-0.3.1.gem (100%) Successfully installed cocoapods-plugins-0.3.1 Fetching: cocoapods-try-0.4.2.gem (100%) Successfully installed cocoapods-try-0.4.2 Fetching: netrc-0.7.8.gem (100%) Successfully installed netrc-0.7.8 … … … Installing ri documentation for fuzzy_match-2.0.4 Parsing documentation for i18n-0.6.11 Installing ri documentation for i18n-0.6.11 Parsing documentation for json_pure-1.8.1 Installing ri documentation for json_pure-1.8.1 Parsing documentation for multi_json-1.10.1 Installing ri documentation for multi_json-1.10.1 Parsing documentation for nap-0.8.0 Installing ri documentation for nap-0.8.0 Parsing documentation for netrc-0.7.8 Installing ri documentation for netrc-0.7.8 Parsing documentation for open4-1.3.4 Installing ri documentation for open4-1.3.4 Parsing documentation for xcodeproj-0.19.4 Installing ri documentation for xcodeproj-0.19.4 18 gems installed
  4. Demo Using CocoaPods • Clone SwiftWeather • https://github.com/JakeLin/ SwiftWeather •

    Uses AFNetwork (Objective- C) via Cocoapods • pod install • bridging header • build setting • But should use Swift library • https://github.com/Alamofire/ Alamofire
  5. Console is Your Friend Useless Useful Worth Watching: “Advanced Swift

    Debugging in LLDB” https://developer.apple.com/videos/wwdc/2014/#410
  6. What are Unit Tests • Automated way to test your

    application • Focus on smallest parts of code • Separate to integration tests • How to run tests… • “Product” -> “Test” menu • ⌘U : Run All Tests • ⌃⌥⌘U : Run Current Test
 Class
  7. And Here’s One I Made Earlier… • https://github.com/sconaty/SwiftTestingTutorial • See

    UnitTestingExample project • Demonstrates verification of JSON parsing using: • SwiftyJSON: https://github.com/SwiftyJSON/SwiftyJSON (★2,200)
  8. Common Gotchas • “unresolved identifier” error when running tests from

    http://natashatherobot.com/swift-unit-testing-tips-and-tricks/
  9. Assertions • Unconditional Fail • Equality Test • Nil Tests

    • Boolean Tests • Exception Tests from Testing with Xcode: Writing Test Classes and Methods
  10. Exercises • Move let json = JSON… out of each

    test into setUp() • Create new testThemeParagraphAlignment test. It should verify… • Initially… • [“alignment”: “right”] sets the default paragraph style to NSTextAlignment.RightTextAlignment • Continue with other possible values… • “center”: NSTextAlignment.CenterTextAlignment • “justified”: NSTextAlignment.JustifiedTextAlignment • “natural”: NSTextAlignment.NaturalTextAlignment
  11. Problems with Unit Tests • Only on Setup() and TearDown()

    per XCTextCase • Can’t easily specify pending tests • Tests can only be structured by classes • Tests often need to be augmented with comments (or assertions with strings) to make purpose clear
  12. Behaviour Driven Development • Swift BDD Options Available… • Quick:

    https://github.com/Quick/Quick (★1,483) • Sleipnir: https://github.com/railsware/Sleipnir (★506)
  13. BDD & Quick • Global setup & teardown using beforeSuite

    and afterSuite • Define “examples” using it • Example groups using describe and context • Define group specific setup & teardown using beforeEach and afterEach • Disable Pending examples and groups • Possible to share sets of example for objects using sharedExamples and itBehavesLike
  14. Adding Quick to Your Project • Using shell, add Quick

    as a git sub-module ! mkdir Vendor git submodule add https://github.com:Quick/Quick.git Vendor/Quick git submodule add https://github.com:Quick/Nimble.git Vendor/Nimble git submodule update --init --recursive • Add Quick.xcodeproj and Nimble.xcodeproj to test target • Link Quick.framework and Nimble.framework • Note, these are already included in the BDDExample project. So use: ! git submodule sync git submodule update --init --recursive
  15. Interesting Parts of Sample • // MARK: to create sections...

    (line 29) • beforeEach and afterEach (lines 64 & 74) • How to read test file from bundle (line 55) • sampleJSON function and how it merges dictionaries (operator overloading) (line 85) • createThemeWithSilentLogging closure (line 16) • “alignment” group which have for loops with inner if (line 127)
  16. Exercises • Create “tail indent” group (similar to “head indent”)

    which… • Verifies “tailIndent” double value can be read from JSON and is stored in theme.defaultParagraphStyle.tailIndent • Verifies if “tailIndent” has invalid string value in JSON then theme.defaultParagraphStyle.tailIndent defaults to 0.0 • Create “writing direction” group (similar to “"line break mode”) which… • Verifies “writingDirection” string in JSON can map string to theme.defaultParagraphStyle.baseWritingDirection. Possible mappings of JSON string value to NSWritingDirection are: • “natural”: NSWritingDirection.Natural • “leftToRight”: NSWritingDirection.LeftToRight • “rightToLeft”: NSWritingDirection.RightToLeft • Verifies if “writingDirection” has invalid string value or double in the JSON then defaults theme.defaultParagraphStyle.baseWritingDirection to .Natural
  17. Useful Swift Links • Swift Reference! • Language Guide •

    Standard Types Reference (numeric, dictionary, array, string) • Language Grammar Reference • Apple SDKs! • AppKit Reference and UIKit Reference • BDD! • Quick & Nimble