Bookmarks

Unit test

An automated check of a small, isolated piece of code, such as a function, method or class; in the ISTQB glossary this is component testing.

Also: unit testing, component testing, module testing

checked 18 Nov

All terms

Unit tests sit at the very bottom of the testing pyramid: small, fast checks that developers run dozens of times a day. Each test exercises one small piece of code in isolation from the rest of the system, so a failure points straight at the latest change.

The ISTQB test level

The ISTQB glossary calls this level component testing: “a test level that focuses on individual hardware or software components” (the glossary lists module testing and unit testing as synonyms). The CTFL v4.0.1 syllabus adds that component testing, also known as unit testing, checks components in isolation, usually with test harnesses or unit test frameworks, and is normally done by the developers themselves in their own environment. The modern meaning, small tests written by the developer, was established by extreme programming practices and the xUnit family of frameworks.

ISTQB levelWhat it checks
Component (unit)individual components in isolation
Integrationinteractions between components or systems
Systemthe whole system: whether it meets the specified requirements

What counts as a unit

There is no single answer: in object-oriented code the unit is most often a class, in functional code a function. Martin Fowler advises treating as a unit whatever the team finds convenient; sometimes it is a group of closely related classes, and that is fine. A more important choice is what to do with the neighbours of the code under test. In “sociable” tests the neighbours stay real; in “solitary” tests they are replaced with test doubles. The terms were proposed by Jay Fields. Doubles take the database, the network and random delays out of the test, so it stays fast and predictable.

What a unit test looks like

pythona unit test for the slugify function
def slugify(title: str) -> str:    return title.strip().lower().replace(" ", "-") def test_slugify():    assert slugify("Unit Test") == "unit-test"    assert slugify("  PyCon ") == "pycon"

Test frameworks (JUnit for Java, unittest in the Python standard library, which is modelled on JUnit, pytest, and Jest for JavaScript) find the tests, run them and report which check failed. Speed is essential here: Fowler names small size, being written by the developer themselves and running noticeably faster than other kinds of tests as common traits of unit tests, and by Kent Beck’s rule a commit suite should finish within ten minutes. Such suites run in CI on every change, which makes unit tests the first automated line of defence against regressions.

Sources

the term is written from these pages

Verified 18 Nov

Not to be confused with

what it is mixed up with and how it differs

  • TDDtest-driven development

    TDD is a way of working: the unit test is written before the code and is first seen to fail; unit tests are useful without TDD too.

  • integration test

    An integration test checks how parts work together; a unit test exercises one part separately from its neighbours.

  • E2Eend-to-end test

    An end-to-end test goes through the whole system following a user scenario; a unit test needs only one module, with no network or interface.

In depth in the references

where the topic is covered in detail