A unit test is a piece of code (usually a method) that invokes another piece of code and checks the correctness of some assumptions afterward. If the assumptions turn out to be wrong, the unit test has failed. A "unit" is a method of a function.

Unit Test RequirementsEdit

  • It is automated and repeatable.
  • It is easy to implement.
  • Once it's written, it stays on for the future.
  • Anyone can run it.
  • It runs at the push of a button.
  • It runs quickly.

Compared to Integration TestsEdit

Integration tests occurs when two or more dependent software modules as a group are tested together. Unit tests are not integration tests, and integration tests are not unit tests.

Methods that perform external communicationEdit

Any method that performs "external communications" (see Three types of methods) should be stubbed out. See discussion on mocking.

Why have both Unit Tests and Functional TestsEdit

  1. A unit test will run faster
  2. A unit test will test the class/component more rigorously and with more test inputs that can be typically tested with a functional test.
  3. When a unit test fails, it's very obvious where the code is that's broken and needs to be fixed, as opposed to with functional tests where debugging can be time consuming to isolate the problem.


Community content is available under CC-BY-SA unless otherwise noted.