Software Engineering Tidbits

Share this post

A good unit test

www.softwareengineeringtidbits.com

A good unit test

Georges El Khoury
Feb 20
8
Share this post

A good unit test

www.softwareengineeringtidbits.com

A good unit test should be:

  1. Idempotent: It’s every engineer favorite word :) Meaning it can be run over and over and is always green.

  2. Independent/Atomic: Meaning it does not dependent on any other tests to run before and can be run at any order or randomly within a test suite

If we are not able to achieve the above, then our unit test will become flaky and a bad (very bad) unit test. It will be flagged with an ignore tag or worst either commented out or deleted all together.

In addition, our unit test should be fast: It should run quickly so it does not delay the development process. This means that all the unit test dependencies should be mocked.

Side note - the only place where not mocking might be okay (and it is debatable) is for an orm heavy app where we can launch a database in memory. I still think it should be mocked but there is a counter argument to be said that not mocking might lead to a more accurate test through relying on actual db data and operations rather than orm mocks.

The unit test title should explain what it does. I recommend testWhenShould pattern in the title but other patterns are okay too. I also recommend having three sections: setup, execute and validate.

In the end, your test should be similar to this:

test*When*Should* {
//setup
.
dependencyOne = mockDependencyOne.return({…});
dependencyTwo = mockDependencyTwo.return({…});
objectToBeTested = new ObjectToBeTested(dependencyOne, dependencyTwo);
//execute
result = objectToBeTested.someFunc();
//validate
assertEquals(result, expectedResult)
}

Unit test code is no less important than product code and should follow the guidelines of elegant code too.

Software Engineering Tidbits
Elegant code
Elegant code is hard to put your finger on, describe or quantify. In his book clean code, Robert Martin spent a full chapter with multiple people trying to do just this. Each of them ended with different definition. Here is my take on it: Clean code is readable, sectionable and effortlessly extensible…
Read more
a year ago · 4 likes · Georges El Khoury

Finally, one last tip I posted about before. When you fix a bug, check why the previous unit tests did not uncover it and update them accordingly.

Software Engineering Tidbits
Fix a bug / Write a unit test
One solid practice to have while coding is to fix a bug and at the same time write a unit test that would have uncovered it. It is a sign of solid experience in software engineering. The direct benefits are to avoid regressions for this bug, to ensure the fix is correct and to increase code coverage and the reliability of the test suite…
Read more
4 months ago · 3 likes · Georges El Khoury


Mastering Software Engineering Course on Maven

If you liked this article, I will be teaching a “Mastering Software Engineering” course on Maven where I will teach hard-learned lessons I acquired developing large-scale products at companies such as Uber, Airbnb, and Microsoft.

View Course


Thanks for reading Software Engineering Tidbits! Subscribe for free to receive new posts and support my work.

Share this post

A good unit test

www.softwareengineeringtidbits.com
Comments
TopNewCommunity

No posts

Ready for more?

© 2023 Georges El Khoury
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing