Unit Testing Guidelines
- Unit tests should not be written to pass:
- They should be written to fail.
- You can make any set of tests pass in minutes but you’re only cheating yourself.
- Tests should only test one thing:
- Test a single method with a single function.
- If not you may be violating the Single Responsibility Principle.
- Readability in your tests: make sure they’re commented and easy to understand.
- Asserts are separated from actions: Your assert should be looking for a result, and not performing logical operations.
- Use concrete inputs: Don’t use any dynamic data for inputs, things like date() can introduce variance.
- Group locations of tests: from a logical standpoint this makes things easier to find when there aren’t errors pointing towards the problem.
- Good tests are isolated from everything: You should have no reliance on other tests, environment settings, etc.
- Do not include private methods: They are implementation and should not be included in Unit Tests.
- Don’t connect to databases or data sources: This is unreliable because you cannot be certain the data served will always be the same.
- No more than one mock per test: Again we’re trying to eliminate points of failure and inconsistencies.
- Unit tests are not integration tests: You want to test results, not implementation.
- Tests must be deterministic: You need a solid predictable result, so if it only passes sometimes, it’s not done.
- Keep your tests idempotent: you should be able to run it multiple times without changing any outcomes.
- Classes only test one class at a time, methods only test one method at a time.
- Include exceptions in your tests: You’re going to have exceptions so don’t ignore them.
- Don’t test functionality of 3rd party libraries with your own tests: Consider mocks to produce consistent results.
- Always limit values: When working with values be mindful of your limits and set them (min max) for maximum consistency.
- Tests should not require configuration or custom setup: Anyone should be able to jump in and make your tests work.
⭐ Approaches to Create Substitute Classes for External Dependencies