-
Unit testing helps you find bugs and produces better, more reliable code.
-
Unit testing helps to develop a tester's mindset.
- A mindset that every piece of code potentially breaks something.
- A mindset that takes edge cases into account by default.
-
Unit testing best practices
- Tests Should Be Readable (Use Sound Naming Conventions)
- Avoid logical conditions such as if, for, while, switch. You should not create any data in test method scope. Only focus result.
- Focus Most Effective Methods
- Unit Tests Should Be Fast
- Avoid Magic Strings
- Tests Should Be Independent
- Each Test Should Test One Thing
- Tests Should Be Deterministic
- Tests Should Not Include Implementation Details (behaviour not details)
- Tests Should Be Part Of The Commit and Build Process (pre-commit hooks to run tests before each commit)
- Use Single Assert Per Test Method
- Use Fake Data/Databases in Testing
- Avoid Test Interdependence (Write individual, independent test methods and put related tests in a single test class)
- Test for Security Issues as Part of Your Unit Tests (SQL Injection for example)
-
Coverage %
- Code coverage percentage of between 60% and 90% (do not become obsessed with 100%)
- Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100
-
Cloud Services Unit test