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.
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.
As you can see, including ExUnit in our notebook is super simple. Now let's right a test.
use ExUnit.Case test do assert Converter.slug() == end end ExUnit.run()
ExUnit.run/0 will execute the test module and print the result below the cell.
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.
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"
That's it. Now every time you change your code, you get a desktop notification.