Unit Testing
Brian Hu
Why Test
• Catch errors
• Help you write code
• Speed up your development
• Cover configurations
• Cover new co-workers
What is a unit test
• Tests ONE thing
• Single “unit” of functionality
• Small, fast, ISOLATED
Unit Tests don’t cover
• Performance
• UI interaction
• Whole system integration
Guidelines
• Testing should be automated and non-interactive
• Let your code JUST pass the test
• Remember to test boundary cases, but also remember that it’s
impossible to test every case
• Provide a random generator when it’s possible
• Keep testing at unit level
• Act as 3rd party class consumer
• Passed tests don’t promise the correctness of code
XCTest
Tutorial Project
http://is.gd/nDMTWU
Instruction
• We have 4 tests in this project: testValidateEmail,
testSubmit, testIsEmpty, testValidatePassword
• I’ll do the “testValidateEmail” step by step for you.
• I have written the tests of “testIsEmpty” and
“testSubmit” for you. Please implement the logic
by yourself.
• Please write the test and implement the logic for
the last test by yourself.
API you may need
#if DEBUG
[NSException raise:@“Invalid Email Exception”
format:@"email is invalid”];
#endif
Password requirement
• length between 6~12
• has at least 1 uppercase letter and 1 lowercase
letter

Unit testing

  • 1.
  • 2.
    Why Test • Catcherrors • Help you write code • Speed up your development • Cover configurations • Cover new co-workers
  • 3.
    What is aunit test • Tests ONE thing • Single “unit” of functionality • Small, fast, ISOLATED
  • 4.
    Unit Tests don’tcover • Performance • UI interaction • Whole system integration
  • 5.
    Guidelines • Testing shouldbe automated and non-interactive • Let your code JUST pass the test • Remember to test boundary cases, but also remember that it’s impossible to test every case • Provide a random generator when it’s possible • Keep testing at unit level • Act as 3rd party class consumer • Passed tests don’t promise the correctness of code
  • 6.
  • 7.
  • 8.
    Instruction • We have4 tests in this project: testValidateEmail, testSubmit, testIsEmpty, testValidatePassword • I’ll do the “testValidateEmail” step by step for you. • I have written the tests of “testIsEmpty” and “testSubmit” for you. Please implement the logic by yourself. • Please write the test and implement the logic for the last test by yourself.
  • 9.
    API you mayneed #if DEBUG [NSException raise:@“Invalid Email Exception” format:@"email is invalid”]; #endif
  • 10.
    Password requirement • lengthbetween 6~12 • has at least 1 uppercase letter and 1 lowercase letter