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

Practical Testing Tips - CocoaConf Chicago, March 2015

Practical Testing Tips - CocoaConf Chicago, March 2015

A smorgasboard of testing tips for iOS, in both Objective-C and Swift. Links are in the slides at the end of the deck.

Ellen Shapiro

March 27, 2015
Tweet

More Decks by Ellen Shapiro

Other Decks in Technology

Transcript

  1. PRACTICAL TESTING TIPS by Ellen Shapiro | Cocoaconf Chicago 2015

    vokal.io | justhum.com | designatednerd.com | @designatednerd
  2. Core Data Demo Recap » When you see somthing that

    can cause a key piece of your app to break » When testing core data, test the NSInMemoryStoreType so you always have a clean database. » Version your databases!
  3. Objective-C Mock Demo Recap » Make a fake thing »

    Tell it what to do » Pass it to the thing you want to test » Make sure the tested thing reacted properly
  4. Swift Inline Class Verification func testThingDoesStuff() { class MockThing :

    Thing { var methodUnderTestWasCalled = false override func methodUnderTest() { //Don't call super! methodUnderTestWasCalled = true } } let testThing = new MockThing() testThing.doSomethingThatShouldCallMethodUnderTest() XCTAssertTrue(testThing.methodUnderTestWasCalled) }
  5. Swift Inline Class Stubs/Spies func testThingDoesStuff() { class StubbedThing :

    Thing { override func methodBeingStubbed() -> String { //Don't call super! return "A Known String!" } } let stub = new StubbedThing() let tested = new ObjectUnderTest() objectUnderTest.thing = stubbed let result = tested.doSomethingThatEventuallyCallsMethodBeingStubbed() XCTAssertTrue(result == "A Known String!") }
  6. HTTP/1.1 200 OK Server: nginx/1.4.6 (Ubuntu) Date: Wed, 12 Nov

    2014 22:10:40 GMT Content-Type: application/json Transfer-Encoding: chunked Connection: keep-alive Vary: Accept X-Frame-Options: SAMEORIGIN Allow: GET, PUT, PATCH, DELETE, HEAD, OPTIONS { "default": true, "expiration_month": "10", "expiration_year": "2018", "last_4": "1234", "card_type": "MasterCard", "id": 2 }
  7. #import "VOKMockURLProtocol.h" @implementation CLTAPIClient (MockData) - (void)setShouldUseMockData:(BOOL)shouldUseMockData { Class mockURLProtocol

    = [VOKMockUrlProtocol class]; NSMutableArray *currentProtocolClasses = [self.sessionConfiguration.protocolClasses mutableCopy]; if (shouldUseMockData) { [currentProtocolClasses insertObject:mockURLProtocol atIndex:0]; } else { [currentProtocolClasses removeObject:mockURLProtocol]; } self.sessionConfiguration.protocolClasses = currentProtocolClasses; } @end
  8. The most useless test I ever inherited: [Note: class prefixes

    have been changed to protect the guilty]
  9. 1. Write a failing test LargerTest.m - (void)testLarger { NSInteger

    larger = [Larger largerOf:2 and:5]; XCTAssertEqual(larger, 5); } Larger.m + (NSInteger)largerOf:(NSInteger)one and:(NSInteger)another { return 0; }
  10. 2. Write code to make it pass LargerTest.m - (void)testLarger

    { NSInteger larger = [Larger largerOf:2 and:5]; XCTAssertEqual(larger, 5); } Larger.m + (NSInteger)largerOf:(NSInteger)one and:(NSInteger)another { if (one > another) { return one; } else { return another; } }
  11. 3-∞. Refactor, but now with the guardrails of the test

    LargerTest.m - (void)testLarger { NSInteger larger = [Larger largerOf:2 and:5]; XCTAssertEqual(larger, 5); } Larger.m + (NSInteger)largerOf:(NSInteger)one and:(NSInteger)another { return MAX(one, another); }
  12. BDD Demo Recap: » Use very descriptive test names. »

    Test the end result, not necessarily how you got there. » Use the XCTest methods you already know. » Use comments if you need the structure of given/ when/then.
  13. KIF

  14. Kif Pros » Write your UI tests in Objective C!

    » Uses Accessibility, so you make your app accessible in the bargain » Get to the bit you need to test » The most widely used library » Very backwards compatible
  15. Kif Cons » Sloooooooooow » Some folks have had issues

    with lookup changing the view hierarchy » Very backwards compatible
  16. UI Testing Recap » If you're swapping out your app

    delegate for tests, make sure you put it back for UI tests » Centralize your localized strings in a convenience class to make strings easier to test » UI and non-UI tests should be seperate targets » Add -AppleLanguages "\(en\)" arguments to the test bit of your application's scheme » Duplicate your scheme and update for every language you support
  17. Official Summary Slide™ » Testing lets you refactor more confidently,

    and find out immediately when you break stuff. » Test as much as you can, however much that is. » Find the testing ideology that works best for you. » UI Testing can give you a great opportunity to see how all the pieces of your app work together. » Have both humans and robots test your app for best results.
  18. Things from me or my employer! #humblebrag » https://github.com/designatednerd/ TestingPlayground

    » https://github.com/designatednerd/FlickrSearcher » https://github.com/designatednerd/ DNSiOSLocalizationTestHelpers » https://github.com/vokal/VOKMockUrlProtocol » https://github.com/vokal/Mocktrofit
  19. Mocking tools! » http://ocmock.org/ » https://github.com/jonreid/OCMockito UI Testing Tools! »

    https://github.com/kif-framework/KIF » https://github.com/Raizlabs/FRY
  20. BDD Tools! » https://github.com/specta/specta » https://github.com/kiwi-bdd/Kiwi » https://github.com/pivotal/cedar Other Things

    I Mentioned! » http://ntoll.org/article/tdd-cargo-cult » http://qualitycoding.org/app-delegate-for-tests/
  21. Photo Credits » Angry mob by Robert Course-Baker: https:// www.flickr.com/photos/29233640@N07/3645211083

    » Robot by Logan Ingalls: https://www.flickr.com/ photos/plutor/847695350 » Lock ("Hodgepodged") by David Goehring https:// www.flickr.com/photos/carbonnyc/4740626368/ » All images of Kif from Futurama used without permission. Sorry, Fox.