DEV Community

Cover image for The 5 principles of Unit Testing

The 5 principles of Unit Testing

David González Rueda on September 14, 2023

Get your software development to the next level with these 5 powerful principles of unit testing! Testing your code is one of the most important as...
Collapse
 
tracygjg profile image
Tracy Gilmore

Excellent post. For those interested in this topic, you might also be interested in the F.I.R.S.T Principles of Unit Testing.

I have started using a forth 'A', on top of the 'AAA', to structure unit tests (Arrange, Affirm, Act & Assert). The Affirm stage is like the Assert stage but before the Act is performed. This enables me to a) ensure the test environment is as we expect, which reduces the potential for false positives (and negatives), b) comparing before with after the Act.

Collapse
 
montells profile image
Michel Sánchez Montells • Edited

Great advices.
Just want to annotate that if one developer still do not have the skills or willing to test, he/she must start only for high coverage, then improve following this guide.
High coverage of not perfect test is better than no test, at least for regresión purpose.
And latest but not less important. WRITE YOUR TEST BEFORE THE CODE

Collapse
 
thenickest profile image
TheNickest

Nice one. I am always happy to see and read through posts about testing and unit testing in particular. The only time I could not nod here was in the beginning of the article where it says „Developing tests should bring great value with very little effort and time investment.“

While tests definitely should bring great value, I’d rather say that any test must be worthwhile and the effort put in justified. In general stating to put little effort and time into them is not necessarily correct but „it depends“.. at least from my experience.

And thank you! Anyone who is aiming at 100% coverage shall please stop doing so. This mostly creates extra effort which is not worth it.

Collapse
 
wraith profile image
Jake Lundberg

Great article! Lots of good content here

Collapse
 
edemagbenyo profile image
Edem Agbenyo

This is an easy-to-understand and great post. If you would like to apply these same principles when building React applications, I recommend this reacttdd.com/.

Collapse
 
lirantal profile image
Liran Tal

Super on-point advice. Good work David!
Another good resource on testing is Yoni's JavaScript Testing guide: github.com/goldbergyoni/javascript...

Collapse
 
tcelestino profile image
Tiago Celestino

Great article. Congrats

Collapse
 
cezarytomczyk profile image
Cezary Tomczyk

I always say: show me your tests, and I will tell you how complex your code is.

Collapse
 
khairunnisaas profile image
Khairunnisaas

i didn't know much about testing... this is very good article! thanks for sharing

Collapse
 
diantantu profile image
Diantantu

Great article I have read a lot about unit testing but I Still cant find content about the correct way of writing them can you suggest good resources like courses or books?