DEV Community

Cover image for Unit Testing, But What is a Unit?

Unit Testing, But What is a Unit?

Adam Gordon Bell
Follow me for Coding, Computers, and Backend stuff.
Updated on ・1 min read

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?

Discussion (3)

dynamicsquid profile image

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?

adamgordonbell profile image
Adam Gordon Bell Author

hmm, good question! It would be cool if the K Langauge were open source:

dynamicsquid profile image