-
Notifications
You must be signed in to change notification settings - Fork 0
What makes a good test
Devrath edited this page Oct 24, 2023
·
5 revisions
- Clear and concise
- Independent
- Repeatable
- Precise
- Speed
- Comprehensive
- Low coupling and high cohesion
- Using abstraction
- Having a way to provide different implementations for the abstraction
- The test case should be easily readable so that it can be easily understood.
- To write a clear and concise test case, we need to have clean code only then we can write proper test cases.
- Tests also act as documentation for the code since you can determine the behavior by looking at tests.
- Basically tests should be independent meaning say you set a variable in a class with one test and check the variable in another test. This violates rule
- So we always need to start a fresh state for each test.
- Iven if we run a test multiple times the output of a test must remain constant.
- Say if it runs once and does not run another time, It is called a
flaky test
. - This indicates that there is some issue with the code.
- If a test fails you need to know which part of the test is causing the failure.
- A good test runs as fast as possible.
- Tests must include all the scenarios and test cases.
- With the above points we can achieve low coupling and high cohesion
- Sometimes one piece of code needs to communicate with another piece of code, In such a scenario abstraction can be used to manage this.
- Even though you have proper abstraction, Sometimes it's difficult to maintain the abstraction without having
dependency injection
, A way you can provide different implementations to the class viaconstructor
.