This document provides an overview of unit testing and guidelines for writing unit tests. It discusses why unit tests are important for catching errors, helping write code, speeding up development, and covering configurations and new team members' code. It defines what a unit test is and what they don't cover. Guidelines provided include automating tests, letting code just pass tests, testing boundaries and randomness, keeping tests at a unit level, and knowing passed tests don't guarantee correctness. It also mentions the XCTest framework and provides a tutorial project and instructions for writing sample unit tests for email and password validation.
2. Why Test
Catch errors
Help you write code
Speed up your development
Cover con鍖gurations
Cover new co-workers
3. What is a unit test
Tests ONE thing
Single unit of functionality
Small, fast, ISOLATED
4. Unit Tests dont cover
Performance
UI interaction
Whole system integration
5. Guidelines
Testing should be automated and non-interactive
Let your code JUST pass the test
Remember to test boundary cases, but also remember that its
impossible to test every case
Provide a random generator when its possible
Keep testing at unit level
Act as 3rd party class consumer
Passed tests dont promise the correctness of code
8. Instruction
We have 4 tests in this project: testValidateEmail,
testSubmit, testIsEmpty, testValidatePassword
Ill 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 may need
#if DEBUG
[NSException raise:@Invalid Email Exception
format:@"email is invalid];
#endif