Automatically run tests on Livebook with desktop notifications

Livebook is a fantastic tool and changed my workflow a lot. I always have it running, and many times during the day, I will validate or check some code I am building there. Experimenting with libraries and connecting to my application to review code is easy.

We can also run ExUnit tests on a cell to experiment with code but keep some unit testing to ensure we are making progress.

To showcase this, let's imagine we are building a converter module that will transform a string into a slug that we can use as URLs.

defmodule Converter do
  def slug(value) do
    String.replace(value, " ", "-")
  end
end

To start writing tests, we don't need to include any dependencies; we only need to start ExUnit, but we will turn off the autorun. We will do this to control when to run the tests and print the result.

ExUnit.start(autorun: false)

As you can see, including ExUnit in our notebook is super simple. Now let's right a test.

defmodule ConverterTest do
  use ExUnit.Case

  test "convert simple string to url slug" do
    assert Converter.slug("this is my string") == "this-is-my-string"
  end
end

ExUnit.run()

The ExUnit.run/0 will execute the test module and print the result below the cell.

test screenshot

Now, let's make this even more enjoyable. We can include desktop notifications after the test run and use a Livebook feature to execute our test cell automatically.

We need to include ex_unit_notifier in our first cell, which says, "Notebook dependencies and setup", and configure ExUnit to use it.

Mix.install([
  {:ex_unit_notifier, "~> 1.3"}
])
ExUnit.configure(formatters: [ExUnit.CLIFormatter, ExUnitNotifier])
ExUnit.start(autorun: false)

We will add the CLIFormatter, the default one, and ExUnitNotifier to display the desktop notifications.

The last part is to automatically evaluate the test cell when we make changes in the other cells. To do this, click on the little cog on the top right of the cell, and select "Reevaluate automatically"

reevaluate automatically config

That's it. Now every time you change your code, you get a desktop notification.

desktop notification example

Run in Livebook

Stay up to date!

Get notified when I publish something new, and unsubscribe at any time.