DEV Community

Discussion on: Why Do You Write Tests?

eldadru profile image
Eldad Rudich

For many reasons

  1. Tests catch bugs: when strategically testing business logic, your test will eventually catch real issues and will save you time and money.
  2. Tests make you write better code: writing tests makes you write testable code - which means - smaller methods, smaller building blogs, DI, simpler code, etc'
  3. Tests make you think about edge cases: when writing tests to your code, you really have to go over the method/class logic and understand what cases are possible and need verification - this way you find new ways your code might fail and have to address that.
  4. Tests allow you/your peers to refactor the code freely: having tests that continuously check your code, allows you to freely change, add or improve the code without the fear of breaking something. without tests, nobody will have the courage to change the code which will eventually become rot.
  5. Tests are live code documentation: tests demonstrate how the code writer planned to use the code, many times with real examples the reader can adopt.

I guess I missed some reasons but those are my top.