Rookian
Rookian

Reputation: 20549

What is THE UNIT in a unit test? And what should it be?

Is a unit a class or a component(more than one class)?

What should be a unit test? How granular should be a unit?

Upvotes: 3

Views: 472

Answers (5)

Gishu
Gishu

Reputation: 136613

The term (similar to many other things associated with Agile) is overloaded (means different things to different people). It has undergone a lot of flux and is now subjective. Some have come up with a new term microtests to prevent confusion.

Earlier it was meant to mean function-level white-box testing. As a rule of thumb, a unit is usually a behavior exposed by a class (usually as a method). Among TDD practitioners, there are two camps

  • interaction-based testing isolates the class from its dependencies (assuming a certain contract between the two). The dependencies are tested via their unit tests. The unit test specifies how the unit behaves w.r.t. to others given certain stimuli
  • state-based testing tests a class along with its real dependencies.

My current understanding is that it's a deliberate choice per unit-test ; one style does not fit all.

Upvotes: 5

CloudyMarble
CloudyMarble

Reputation: 37566

A unit is the smallest testable part of the application, usually a function.

Upvotes: 5

GorillaPatch
GorillaPatch

Reputation: 5057

I would put it like this: a unit in a unit test is an entity for which a defined output is expected for a known input. The level of granularity however can vary dramatically: from a function to a class to the whole program.

Upvotes: 3

Rachel McMahan
Rachel McMahan

Reputation: 392

It could also be smaller; a unit test for a single method.

Upvotes: 2

Shaggy Frog
Shaggy Frog

Reputation: 27601

It can be a class. It can be more than one class. It can be the entire system. It's a logical piece that takes input and gives output -- however those are defined for that component.

Upvotes: 2

Related Questions