DEV Community

Cover image for Unit Testing, But What is a Unit?
Adam Gordon Bell
Adam Gordon Bell

Posted on • Edited on

2

Unit Testing, But What is a Unit?

Unit tests are tests that are testing the smallest possible pieces of code in isolation.

What then is a unit?

The term unit comes from mathematics. The number 1 is considered the unit as it is the smallest natural number. It is the smallest whole positive number.

By analogy, a unit of your source code is the smallest piece of code that is logically separate from the rest of the code. It is a whole piece, a logically distinct area code, and it is the smallest possible such piece.

In most programming languages your unit is going to be a function or method call.

The great thing about unit testing is that if your code is structured in small independent pieces then writing tests for them can be quite easy. This ease-of-writing means that unit testing can be done as you develop features.

In comparison to other forms of testing, the execution time of unit tests is quite small. This means that you can run unit tests very frequently. As software matures a suite of unit tests is a powerful tool for preventing regressions and easing maintenance costs.

But unit testing is not the only game in town. There are also integration tests and performance tests and much more.

It is interesting though that unit in the term unit testing has this relationship to the unit in number theory.

What do you think?

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (3)

Collapse
 
dynamicsquid profile image
DynamicSquid

Oh, I noticed on your profile it says you like open source. What's something that isn't open source, but you'd like to see being made open source?

Collapse
 
adamgordonbell profile image
Adam Gordon Bell

hmm, good question! It would be cool if the K Langauge were open source: en.wikipedia.org/wiki/K_(programmi...

Collapse
 
dynamicsquid profile image
DynamicSquid

Interesting!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay