Testing Our Code

Let’s learn how to write tests for the code in our counter project.

Importance of testing

One of the benefits of structuring our project into core and boundary layers is that our code organization will simplify testing. With a basic API layer that does most of the business logic, we’ll be able to write tests to exercise our business code thoroughly should we choose to do so. We’ll be able to represent our testing concepts in any way we choose, and we’ll discuss a few strategies as the course progresses.

Commencing our tests

We will focus on unit testing here with ExUnit—a unit testing framework bundled with Elixir—but the same principles apply to property-based testing. This philosophy allows us to specify the properties of our code so that the computer can generate many different tests. For now, let’s write a simple test for our counter. We’ll start with the business logic.

Since we have only a single function, testing it should go quickly. We’ll modify the file /test/counter_test.exs and make it look like this:

Get hands-on with 1200+ tech skills courses.