Unit Testing
Learn about unit tests, the intuition behind them, and their advantages and disadvantages.
We'll cover the following
Overview of unit tests
Let’s start with unit tests. Here’s an example to give you an intuitive sense of what they are.
A project’s code is a complex web of transformations called functions or class methods, which transform some input into some output. Some of these transformations are deterministic, and some of them are random.
Why not check these transformations at every step, to see whether each result corresponds to the expected one in some sense (either a complete match or satisfaction of certain conditions)? These simple, function-wise tests are called unit tests, where a function refers to a unit.
Code example
Let’s look at some examples of unit tests. In the code below, we define a function, average
, that computes the average of some numbers. We then test if it gives us the expected output while passing some input.
Get hands-on with 1400+ tech skills courses.