Principles and Guidelines
1.1、F.I.R.S.T Principles
The unit testing case should follow these principles no matter how you implement it.
Fast
- All of these including setup, the actual test and tear down should execute really fast (milliseconds) as you may have thousands of tests in your entire project.
Isolated/Independent
- For any given unit test, for its environment variables or for its setup. It should be independent of everything else should so that it results is not influenced by any other factor.
- Should follow the 3 A’s of tes__ting: Arrange, Act, Assert.
- In some literature, it’s also called as Given, when, then.
Repeatable
- tests should be repeatable and deterministic, their values shouldn’t change based on being run on different environments.
- Each test should set up its own data and should not depend on any external factors to run its test.
Self-validating
- you shouldn’t need to check manually, whether the test passed or not.
Thorough
- should cover all the happy paths
- try covering all the edge cases, where the author would feel the function would fail.
- test for illegal arguments and variables.
- test for security and other issues
- test for large values, what would a large input do their program.
- should try to cover every use case scenario and not just aim for 100% code coverage.