Skip to content

What makes a good test

Devrath edited this page Oct 24, 2023 · 5 revisions

github-header-image (1)

Good test involves

  • 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

Clear and concise

  • 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.

Independent

  • 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.

Repeatable

  • 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.

Precise

  • If a test fails you need to know which part of the test is causing the failure.

Speed

  • A good test runs as fast as possible.

Comprehensive

  • Tests must include all the scenarios and test cases.

Low coupling and high cohesion

  • With the above points we can achieve low coupling and high cohesion

Using abstraction

  • Sometimes one piece of code needs to communicate with another piece of code, In such a scenario abstraction can be used to manage this.

Having a way to provide different implementations for the abstraction

  • 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 via constructor.