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

TDD Workshop

TDD Workshop

Introduction to tests, TDD and Specta testing framework.

Pawel Dudek

April 26, 2014
Tweet

More Decks by Pawel Dudek

Other Decks in Programming

Transcript

  1. Wikipedia, from a book on tests “Unit testing is a

    method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures are tested to determine if they are fit for use.” 2
  2. A test is not a unit test if… • It

    talks to a database • It communicates across network • It touches the file system • You have to do special things to your environment to run it (edit config files etc)
  3. A test is not a unit test if… • It

    talks to a database • It communicates across network • It touches the file system • You have to do special things to your environment to run it (edit config files etc)
  4. A test is not a unit test if… • It

    talks to a database • It communicates across network • It touches the file system • You have to do special things to your environment to run it (edit config files etc)
  5. A 100 ms tests is a very slow test. Thus

    touching file system, network, database etc is a no no.
  6. TDD is a great way to determine how complex your

    code has become. ! You just have to listen. 34
  7. This always points to an overcomplicated design. ! And your

    tests are here to point that out. Very clearly.
  8. Are unit tests an invaluable tool for writing great software?

    Heck yes.  Am I going to produce a poor product if I can’t unit test? Hell no. Jonathan Rasmusson 41 http://agilewarrior.wordpress.com/2012/10/06/its-not-about-the-unit-tests/
  9. BDD builds upon TDD by formalising the good habits of

    the best TDD practitioners. Matt Wynne, XP Evangelist 46 http://blog.mattwynne.net/2012/11/20/tdd-vs-bdd/
  10. Good habits 47 • Work outside-in • Use examples to

    clarify requirements • Use ubiquitous language Thanks Matt!!
  11. describe(@"NSNumber", ^{ describe(@"when created with the default constructor", ^{ it(@“should

    have 0 as contained int value", ^{ NSNumber *number = [[NSNumber alloc] init]; expect([number integerValue]).to.equal(0); }); }); context(@"when constructed with an int", ^{ ! it(@“should have 42 as contained int value", ^{ NSNumber *number = [[NSNumber alloc] initWithInt:42]; expect([number integerValue]).to.equal(42); }); }); });
  12. afterEach(^{ appDelegate = nil; }); beforeEach(^{ appDelegate = [[AppDelegate alloc]

    init]; }); it(@"should have a window", ^{ expect(appDelegate.window).to.beKindOf([UIWindow class]); });
  13. afterEach(^{ appDelegate = nil; }); beforeEach(^{ appDelegate = [[AppDelegate alloc]

    init]; }); it(@"should have a window", ^{ expect(appDelegate.window).to.beKindOf([UIWindow class]); }); 1 2 3
  14. Focusing tests fdescribe(@"Example specs on NSString", ^{ fit(@"lowercaseString returns a

    new string with everything in lower case", ^{ fcontext(@"init with damping", ^{
  15. x’ing tests xdescribe(@"Example specs on NSString", ^{ xit(@"lowercaseString returns a

    new string with everything in lower case", ^{ xcontext(@"init with damping", ^{
  16. (…) ! -[SpecSuiteName passing_spec_name] Test Case '-[SpecSuiteName passing_spec_name]' started. Test

    Case '-[SpecSuiteName passing_spec_name]' passed (0.271 seconds). ! -[SpecSuiteName failling_spec_name] Test Case '-[SpecSuiteName failling_spec_name]' started. Test Case '-[SpecSuiteName failling_spec_name]' failed (0.002 seconds). (…) ! Executed 2 tests, with 1 failure (1 unexpected) in 0.273 (0.278) seconds ! 2 tests; 0 skipped; 1 failure; 1 exception; 0 pending
  17. (…) ! -[SpecSuiteName passing_spec_name] Test Case '-[SpecSuiteName passing_spec_name]' started. Test

    Case '-[SpecSuiteName passing_spec_name]' passed (0.271 seconds). ! -[SpecSuiteName failling_spec_name] Test Case '-[SpecSuiteName failling_spec_name]' started. Test Case '-[SpecSuiteName failling_spec_name]' failed (0.002 seconds). (…) ! Executed 2 tests, with 1 failure (1 unexpected) in 0.273 (0.278) seconds ! 2 tests; 0 skipped; 1 failure; 1 exception; 0 pending
  18. (…) ! -[SpecSuiteName passing_spec_name] Test Case '-[SpecSuiteName passing_spec_name]' started. Test

    Case '-[SpecSuiteName passing_spec_name]' passed (0.271 seconds). ! -[SpecSuiteName failling_spec_name] Test Case '-[SpecSuiteName failling_spec_name]' started. Test Case '-[SpecSuiteName failling_spec_name]' failed (0.002 seconds). (…) ! Executed 2 tests, with 1 failure (1 unexpected) in 0.273 (0.278) seconds ! 2 tests; 0 skipped; 1 failure; 1 exception; 0 pending
  19. (…) ! -[SpecSuiteName passing_spec_name] Test Case '-[SpecSuiteName passing_spec_name]' started. Test

    Case '-[SpecSuiteName passing_spec_name]' passed (0.271 seconds). ! -[SpecSuiteName failling_spec_name] Test Case '-[SpecSuiteName failling_spec_name]' started. Test Case '-[SpecSuiteName failling_spec_name]' failed (0.002 seconds). (…) ! Executed 2 tests, with 1 failure (1 unexpected) in 0.273 (0.278) seconds ! 2 tests; 0 skipped; 1 failure; 1 exception; 0 pending
  20. (…) ! -[SpecSuiteName passing_spec_name] Test Case '-[SpecSuiteName passing_spec_name]' started. Test

    Case '-[SpecSuiteName passing_spec_name]' passed (0.271 seconds). ! -[SpecSuiteName failling_spec_name] Test Case '-[SpecSuiteName failling_spec_name]' started. Test Case '-[SpecSuiteName failling_spec_name]' failed (0.002 seconds). (…) ! Executed 2 tests, with 1 failure (1 unexpected) in 0.273 (0.278) seconds ! 2 tests; 0 skipped; 1 failure; 1 exception; 0 pending
  21. Jon Reid “AppCode definitely empowers TDD. What I didn't get

    until I saw someone's screencast is to really lean on Extract Variable to reduce typing.”