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

(Unit) Testing iOS Apps

(Unit) Testing iOS Apps

Pawel Dudek

January 24, 2014
Tweet

More Decks by Pawel Dudek

Other Decks in Programming

Transcript

  1. (Unit) Testing iOS Apps
    Paweł Dudek

    View Slide

  2. So a QA guys walks
    into your room…

    View Slide

  3. That’s a lot of wasted
    time.

    View Slide

  4. Could it be saved?

    View Slide

  5. YES!!!
    If only we had a better test coverage...

    View Slide

  6. One of the many reasons
    why you want to have
    tests.
    What are the others?

    View Slide

  7. Reasons for testing
    • Saved time

    • Better codebase

    • Faster development cycles

    • Being “confident” about your code

    • More saved time

    View Slide

  8. More saved
    time? What?

    View Slide

  9. Common
    misconceptions

    View Slide

  10. Common
    misconceptions
    • “It will take longer to write code” or “Time
    spent writing/refactoring tests is time lost”

    • “It will take more time to modify existing
    system”

    View Slide

  11. Am I going to write
    poor software if I don’t
    do tests?

    View Slide

  12. 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

    View Slide

  13. Now that we know
    that writing tests is a
    good idea...

    View Slide

  14. How can we do it?

    View Slide

  15. • You will feel confused

    • You won’t know how to start

    • You will need help

    • Conclusion: it’s not easy to start

    View Slide

  16. Tips
    • Never think of tests as tests

    • Think of a scenario, behavior, example

    • Grab a mature project from github with
    tests included

    • Find someone experienced and ask
    questions

    • Program in pairs!

    View Slide

  17. Get on with it!
    How can we test?

    View Slide

  18. Working
    effectively with
    legacy code
    Michael C. Feathers

    View Slide

  19. TDD
    • Test Driven Development

    • Red, Green, Refactor

    • Write failing test first

    • Fix it

    • Refactor

    View Slide

  20. BDD
    Behavior Driven Development

    View Slide

  21. How does BDD differ
    from TDD?

    View Slide

  22. BDD builds upon TDD by formalising
    the good habits of the best TDD
    practitioners.
    Matt Wynne,

    XP Evangelist

    View Slide

  23. Good habits

    View Slide

  24. Unit Tests

    View Slide

  25. OCUnit

    View Slide

  26. OCUnit Syntax
    • All test classes inherit from SenTestCase

    • All tests begin with test

    • Setup and teardown method

    • Everything else is ignored by testing
    framework

    • Means you can use as additional setup
    methods!

    View Slide

  27. -(void)testFullName {
    Person *person = [Person person];
    person.firstName = @"Mariusz";
    person.secondName = @"Testowniczek";
    NSString *fullName = [person fullName];
    NSString *expectedName = @"Mariusz Testowniczek";
    STAssertTrue([fullName isEqualToString:expectedName], @"");
    }

    View Slide

  28. OCUnit vs XCTest

    View Slide

  29. OCUnit vs XCTest

    View Slide

  30. Behavior Tests

    View Slide

  31. Kiwi and Cedar

    View Slide

  32. View Slide

  33. SPEC_BEGIN(PersonSpec)
    !
    describe(@"Person", ^{
    __block Person *person;
    !
    beforeEach(^{
    person = [[Person alloc] init];
    person.firstName = @"Mariusz";
    person.lastName = @"Fixture Last Name";
    });
    !
    describe(@"full name", ^{
    !
    __block NSString *fullName;
    !
    beforeEach(^{
    fullName = [person fullName];
    });
    !
    it(@"should return the full name", ^{
    expect(fullName).to(equal(@"Mariusz Testowniczek"));
    });
    });
    });
    !
    SPEC_END

    View Slide

  34. iOS Testing Tips

    View Slide

  35. Testing UI Layout

    View Slide

  36. System Singletons

    View Slide

  37. Common caveats

    View Slide

  38. Example

    View Slide

  39. More advanced topics

    View Slide

  40. Summary

    View Slide

  41. Summary
    • Testing is a great way to help developers

    • Better codebase, faster iterations

    • Invaluable for larger projects

    View Slide

  42. Resources & Contact
    @eldudi
    github.com/paweldudek
    [email protected]
    Code Examples
    Contact

    View Slide