Why do we need unit tests?
Unit tests give us a high degree of confidence that our code behaves like we expect it to.
This is important in two moments: when first writing a piece of code and when we need to change the code later on.
What are unit tests?
Unit tests verify that the functions in the project produce the outputs we expect for a given input.
Lets drill down on that a bit.
The unit tests should not be on a separate project. This is important because unit tests are closely related to the source code, and having them on a separate project would make it much harder to implement and maintain the tests.
The purpose of unit tests is to validate individual functions. The fact that they only test individual functions increases test speed so we can easily run the whole test suite locally. That also makes them easier to write, maintain, and debug. This is not to say that other types of tests, like integration tests, are not important. We just don’t want to mix unit testing with other types of tests. If we do, we risk losing the advantages of unit tests.
Unit tests fulfil their purpose by performing assertions. Assertions are equality checks. We check that the output of a function is what we expect given the passed input. In modern libraries, assertions are a bit fancier. We’ll expand on this later.