---
title: "Definition of a Unit Test"
url: "https://www.artofunittesting.com/definition-of-a-unit-test.md"
canonical: "https://www.artofunittesting.com/definition-of-a-unit-test"
tags: []
author: "Roy Osherove"
description: "The Art of Unit Testing, 3rd Edition, by Roy Osherove (Manning): readable, maintainable, trustworthy unit tests. Plus hands-on TDD workshops in C#, JavaScript, Java and Kotlin."
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Definition of a Unit Test

Article · Free resource

# Unit test: a definition

> Related: [Test Review Guidelines](https://www.artofunittesting.com/unit-testing-review-guidelines/)

I used to feel that a ‘unit’ was the smallest possible part of a code base (a method, really). But in the past couple of years I’ve changed my mind. Here’s how I define a unit test, as of October 2011:

**A unit test is an automated piece of code that invokes a unit of work in the system _and then checks a single assumption about the behavior of that unit of work_**.

A unit of work is a single logical functional use case in the system that can be invoked by some public interface (in most cases). A unit of work can span a single method, a whole class or multiple classes working together to achieve one single logical purpose that can be verified.

**A good unit test is:**

- Able to be fully **automated**
- Has full control over all the pieces running (Use mocks or stubs to achieve this isolation when needed)
- Can be run in any **order** if part of many other tests
- Runs in **memory** (no DB or File access, for example)
- **Consistently** returns the same result (You always run the same test, so no random numbers, for example. save those for integration or range tests)
- Runs **fast**
- Tests a **single logical concept** in the system
- **Readable**
- **Maintainable**
- **Trustworthy** (when you see its result, you don’t need to debug the code just to be sure)

I consider any test that doesn’t live up to all these as an integration test and put it in its own “integration tests” project.

## Go deeper: the book

The Art of Unit Testing, 3rd Edition covers these ideas in depth, with examples in JavaScript.

[Buy the book (opens in a new tab)](https://go.osherove.com/aout3)

Print + ebook from Manning · Buy links go to Manning and include Roy’s affiliate code.


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
