The "Unit" in "Unit Tests": What Google says

You already know from my previous post that the "unit" in "unit tests" is the test. That emphasizes that either of our tests should be able to run on its own.

Dorota Biernacka from Akamai made an interesting note.

Imagine you're attending a job interview.

The manager running the interview wanted to ask you about unit tests.

They wanted to prepare well, so they googled what the unit in "unit tests" is, to check if their knowledge is right.

Multiple front-page answers in Google said the "unit" in "unit tests" is a class or function.

Other responses weren't persuasive enough to convince them there may be anything else in a program that could constitute a "unit", other than a class, a method, or a function.

So they ask you, and you answer as Kent Beck said: "The unit (referring to units of isolation) is the test.".

The manager is not convinced. They read multiple Google results that indicated the "unit" must be a part of the application. "How can a test be a 'unit'?" - they wonder.

This way, some managers could reject you if you use that answer, even though that is indeed the only correct one!

Google's answers

Let's review the front-page answers from Google (https://www.google.com/search?q=the unit in unit test) [1].

The 'units' in a unit test can be functions, procedures, methods, objects, or other entities in an application's source code. Each development team decides what unit is most suitable for understanding and testing their system. For example, object-oriented design tends to treat a class as the unit.

-- Result 1 (incorrect - the units are not "entities in an application's source code"; they are in the test suite!) https://brightsec.com/blog/unit-testing/

A unit is the smallest testable part of an application.

-- Result 2 (incorrect - the units are not in the "application"; you would have to consider a "requirement"/"feature" a "part of an application" for this to count as correct), quoting Wikipedia, https://stackoverflow.com/a/1066619

Unit is defined as a single behaviour exhibited by the system under test (SUT), usually corresponding to a requirement. While it may imply that it is a function or a module (in procedural programming) or a method or a class (in object-oriented programming) it does not mean functions/methods, modules or classes always correspond to units. From the system-requirements perspective only the perimeter of the system is relevant, thus only entry points to externally-visible system behaviours define units.

– Result 3 (correct!), Wikipedia, https://en.wikipedia.org/wiki/Unit_testing#Unit, quoting "Test-Driven Development by Example" (2002) by Kent Beck, the original introduction to Test-Driven Development

The word unit in unit testing refers to units of isolation, that is, unit tests are isolated, at some level, from the rest of the codebase.

– Result 4 (correct!), https://www.infoq.com/articles/unit-testing-approach/

A unit test is a way of testing the smallest piece of code that can be logically isolated in a software application.

– Result 5 (incorrect!), https://smartbear.com/learn/automated-testing/what-is-unit-testing/

A “unit” typically refers to the smallest testable and independent component of a software application.

– Result 6 (wrong! Notably, substitute "component" for "feature" and it would be right), https://medium.com/@sugumar.p/understanding-the-unit-in-unit-testing-4ab18f8a45a1

In unit testing, a unit isn't an element of structure. In unit testing, a unit is an individually observable element of BEHAVIOR.

– Result 7 (correct, and concise; touché!), https://hackmd.io/@pierodibello/What-is-a-Unit-in-Unit-Testing

Our unit test should be large enough that you can assert something meaningful, but small enough that you can quickly read & assess it.

– Result 8 (on the right track. The title says it even better: "What's the “unit” in unit testing and why is it not a class"), by Dennis Doomen, https://ddoomen.medium.com/whats-the-unit-in-unit-testing-and-why-is-it-not-a-class-de1f1b846d86

A unit test is a block of code that verifies the accuracy of a smaller, isolated block of application code, typically a function or method.

– Result 9 (wrong! Nice try, AWS), https://aws.amazon.com/what-is/unit-testing/

A unit test is an automated piece of code that invokes the unit of work being tested, and then checks some assumptions about a single end result.

– Result 10 (correct, on condition that you note the "unit of work" is not a "unit of program structure"), https://www.blinkingcaret.com/2016/04/27/what-exactly-is-a-unit-in-unit-testing/

Summary

Overall, we got 3 correct answers correct, 2 somewhat correct (if you dig deep enough), and 5 incorrect. The first correct answer was in result 3 (not the top result).

We cannot be sure an interviewer will know the correct answer. Very likely they will think they know – like I did up to the moment I learned otherwise not so long ago.

What that means for you is: if asked during an interview, stick to an answer that includes the source for the correct answer (Kent Beck, book "Test-Driven Development by Example"), and a remark that multiple front-page Google results suggest otherwise.

One example of an answer like that would be:

The "unit" is a single test - in the sense it should not be dependent on any other test passing or failing.

That is called "test isolation". It promotes designing programs in a way that allows calling parts of the program without requiring unrelevant parts of the program to work (or even exist!).

That is the point of view of the inventor of TDD, Kent Beck.

Nevertheless, numerous front-page sources point to classes or functions as "units".


  1. Accessed on December 21st, 2024 ↩︎