top of page
Search

Always write Unit Tests

Writer: Hareesh Lakshmi NarayananHareesh Lakshmi Narayanan

Unit test code should be treated as production code and unit tests should be considered a product feature. Unit tests ensure defects are caught early. Rectifying defects downstream through integration tests, pre-production or production version of the product takes lot of time and effort.


According to an unknown survey, every line of code written is read and refactored 10x times. Having strong set of unit tests improves design and code quality by forcing us to think in terms of loosely coupled, short and simple methods. This makes reading code a pleasant experience. Unit tests provides a safety net to prevent regression when refactoring.


Writing good unit tests is a skill gained with experience and below are list of things unit tests should follow


- Automated

- Simple

- Self-contained

- Assert result

- Fast

- Each test should have a single purpose

- Test method name should clearly convey what test is performed on what method

- Should use standard Dependency Injection frameworks used in the product to inject mocks or stubs

- Should not talk to external database

- Should not communicate across the network

 
 
 

Recent Posts

See All

Rust - variable shadow

Rust allows to declare a new variable with the same as previously declared variable. It is referred to as shadowing. Rust also allows...

Joins

Joining datasets or tables is a common operation these days and Join type determines what rows will be in the result set. This post is a...

コメント


Post: Blog2_Post

hareesh.lakshminarayanan(at)gmail.com

bottom of page