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

Unit testing with Kiwi - CocoaConf San Jose 2013

Unit testing with Kiwi - CocoaConf San Jose 2013

Find out why unit testing will make your developer life easier and how to get started testing your own app using Kiwi, a popular open-source unit testing library. We look at how to get setup with Kiwi. Then we learn the fundamentals of testing, along with some common patterns and idioms.

Pete Hodgson

April 18, 2013
Tweet

More Decks by Pete Hodgson

Other Decks in Programming

Transcript

  1. 1. write code 2. submit stack of cards 3. wait

    (probably overnight) 4. find out if your program worked Thursday, April 18, 13
  2. what is kiwi? open-source tool builds on top of SenTest

    adds nicer ways of organizing tests adds nicer ways to make assertions adds ability to mock/stub Thursday, April 18, 13
  3. #import "Kiwi.h" SPEC_BEGIN(MyFirstSpec) describe(@"Basic Arithmetic", ^{ it(@"can increment 12",^{ [[theValue(12+1)

    should] equal:theValue(13)]; }); }); SPEC_END myFirstSpec.m Thursday, April 18, 13
  4. #import "Kiwi.h" SPEC_BEGIN(MyFirstSpec) describe(@"Basic Arithmetic", ^{ it(@"can increment 12",^{ [[theValue(12+1)

    should] equal:theValue(13)]; }); }); SPEC_END myFirstSpec.m Thursday, April 18, 13
  5. #import "Kiwi.h" SPEC_BEGIN(MyFirstSpec) describe(@"Basic Arithmetic", ^{ it(@"can increment 12",^{ [[theValue(12+1)

    should] equal:theValue(13)]; }); }); SPEC_END myFirstSpec.m Thursday, April 18, 13
  6. describe(@"Station", ^{ it(@"loads from JSON correctly",^{ NSArray *rawStations = @[

    @{@"abbr":@"s1",@"name":@"station one"}, @{@"abbr":@"s2",@"name":@"station two"}, ]; NSArray *stations = [Station loadStations:rawStations]; [[theValue(stations.count) should] equal:theValue(2)]; [[[stations[0] abbr] should] equal:@"s1"]; [[[stations[0] name] should] equal:@"station one"]; [[[stations[1] abbr] should] equal:@"s2"]; [[[stations[1] name] should] equal:@"station two"]; }); }); Thursday, April 18, 13
  7. describe(@"Station", ^{ it(@"loads from JSON correctly",^{ NSArray *rawStations = @[

    @{@"abbr":@"s1",@"name":@"station one"}, @{@"abbr":@"s2",@"name":@"station two"}, ]; NSArray *stations = [Station loadStations:rawStations]; [[theValue(stations.count) should] equal:theValue(2)]; [[[stations[0] abbr] should] equal:@"s1"]; [[[stations[0] name] should] equal:@"station one"]; [[[stations[1] abbr] should] equal:@"s2"]; [[[stations[1] name] should] equal:@"station two"]; }); }); Thursday, April 18, 13
  8. describe(@"Station", ^{ it(@"loads from JSON correctly",^{ NSArray *rawStations = @[

    @{@"abbr":@"s1",@"name":@"station one"}, @{@"abbr":@"s2",@"name":@"station two"}, ]; NSArray *stations = [Station loadStations:rawStations]; [[theValue(stations.count) should] equal:theValue(2)]; [[[stations[0] abbr] should] equal:@"s1"]; [[[stations[0] name] should] equal:@"station one"]; [[[stations[1] abbr] should] equal:@"s2"]; [[[stations[1] name] should] equal:@"station two"]; }); }); Thursday, April 18, 13
  9. describe(@"Station", ^{ it(@"loads from JSON correctly",^{ NSArray *rawStations = @[

    @{@"abbr":@"s1",@"name":@"station one"}, @{@"abbr":@"s2",@"name":@"station two"}, ]; NSArray *stations = [Station loadStations:rawStations]; [[theValue(stations.count) should] equal:theValue(2)]; [[[stations[0] abbr] should] equal:@"s1"]; [[[stations[0] name] should] equal:@"station one"]; [[[stations[1] abbr] should] equal:@"s2"]; [[[stations[1] name] should] equal:@"station two"]; }); }); Thursday, April 18, 13
  10. describe(@"Station", ^{ it(@"loads from JSON correctly",^{ NSArray *rawStations = @[

    @{@"abbr":@"s1",@"name":@"station one"}, @{@"abbr":@"s2",@"name":@"station two"}, ]; NSArray *stations = [Station loadStations:rawStations]; [[theValue(stations.count) should] equal:theValue(2)]; [[[stations[0] abbr] should] equal:@"s1"]; [[[stations[0] name] should] equal:@"station one"]; [[[stations[1] abbr] should] equal:@"s2"]; [[[stations[1] name] should] equal:@"station two"]; }); }); Arrange Thursday, April 18, 13
  11. describe(@"Station", ^{ it(@"loads from JSON correctly",^{ NSArray *rawStations = @[

    @{@"abbr":@"s1",@"name":@"station one"}, @{@"abbr":@"s2",@"name":@"station two"}, ]; NSArray *stations = [Station loadStations:rawStations]; [[theValue(stations.count) should] equal:theValue(2)]; [[[stations[0] abbr] should] equal:@"s1"]; [[[stations[0] name] should] equal:@"station one"]; [[[stations[1] abbr] should] equal:@"s2"]; [[[stations[1] name] should] equal:@"station two"]; }); }); ACT Thursday, April 18, 13
  12. describe(@"Station", ^{ it(@"loads from JSON correctly",^{ NSArray *rawStations = @[

    @{@"abbr":@"s1",@"name":@"station one"}, @{@"abbr":@"s2",@"name":@"station two"}, ]; NSArray *stations = [Station loadStations:rawStations]; [[theValue(stations.count) should] equal:theValue(2)]; [[[stations[0] abbr] should] equal:@"s1"]; [[[stations[0] name] should] equal:@"station one"]; [[[stations[1] abbr] should] equal:@"s2"]; [[[stations[1] name] should] equal:@"station two"]; }); }); ASSERT Thursday, April 18, 13
  13. #import "Kiwi.h" #import "UpcomingDeparture.h" SPEC_BEGIN(UpcomingDepartureSpec) describe(@"[UpcomingDeparture etdToDisplay]", ^{ it(@"is correct

    for an etd of 5",^{ UpcomingDeparture *upcomingDeparture = [[UpcomingDeparture alloc] initWithDestinationName:@"blah" etd:@(5)]; [[[upcomingDeparture etdToDisplay] should] equal:@"5 mins"]; }); }); SPEC_END Thursday, April 18, 13
  14. #import "Kiwi.h" #import "UpcomingDeparture.h" SPEC_BEGIN(UpcomingDepartureSpec) describe(@"[UpcomingDeparture etdToDisplay]", ^{ it(@"is correct

    for an etd of 5",^{ UpcomingDeparture *upcomingDeparture = [[UpcomingDeparture alloc] initWithDestinationName:@"blah" etd:@(5)]; [[[upcomingDeparture etdToDisplay] should] equal:@"5 mins"]; }); it(@"is correct for an etd of 1",^{ UpcomingDeparture *upcomingDeparture = [[UpcomingDeparture alloc] initWithDestinationName:@"blah" etd:@(1)]; [[[upcomingDeparture etdToDisplay] should] equal:@"1 min"]; }); }); SPEC_END Thursday, April 18, 13
  15. #import "Kiwi.h" #import "UpcomingDeparture.h" SPEC_BEGIN(UpcomingDepartureSpec) describe(@"[UpcomingDeparture etdToDisplay]", ^{ it(@"is correct

    for an etd of 5",^{ UpcomingDeparture *upcomingDeparture = [[UpcomingDeparture alloc] initWithDestinationName:@"blah" etd:@(5)]; [[[upcomingDeparture etdToDisplay] should] equal:@"5 mins"]; }); it(@"is correct for an etd of 1",^{ UpcomingDeparture *upcomingDeparture = [[UpcomingDeparture alloc] initWithDestinationName:@"blah" etd:@(1)]; [[[upcomingDeparture etdToDisplay] should] equal:@"1 min"]; }); }); SPEC_END Thursday, April 18, 13
  16. it(@"is correct for an etd of 0",^{ UpcomingDeparture *upcomingDeparture =

    [[UpcomingDeparture alloc] initWithDestinationName:@"blah" etd:@(0)]; [[[upcomingDeparture etdToDisplay] should] equal:@"now"]; }); Thursday, April 18, 13
  17. it(@"is correct for an etd of 0",^{ UpcomingDeparture *upcomingDeparture =

    [[UpcomingDeparture alloc] initWithDestinationName:@"blah" etd:@(0)]; [[[upcomingDeparture etdToDisplay] should] equal:@"now"]; }); Thursday, April 18, 13
  18. Resources • http://slides.thepete.net • http://github.com/allending/Kiwi • the “up and running

    with kiwi” guide • My example BART app on github • NSScreencast on Kiwi (it’s free!) • me: @ph1, http://thepete.net, the bar this slide deck Thursday, April 18, 13