How do you create a test suite in elixir? Time to find out.

One of the Elixir cards holds a clue:

A quick google search throws up Doctests from the getting started guide, which is an impressive feature, but nobody is going to be using my reimplementations of Elixir functions, and I would like some code highlighting on my test code.

Next I find a very helpful series from Elixir School which covers writing test script files. If I take a step back, then I can see that I'm poking into realms outside of the REPL and I need to look at creating Elixir projects. Elixir School mentions a previous post that generates a project so I'm going to do that and have a nose around in what it produces.


$ mix new cards

It feels familiar to working in a Rails project, but the contents of the test/cards_helper.exs surprised me:


ExUnit.start()

I assume that this is starting up another process, and to see how that worked I dug out the source code and found a heap of things that I didn't understand, but at the core of it there was a parent and child process mechanism going on for each test case, with this being the parent process. I thought that sending Elixir stuff back to the parent process was a neat trick:


send parent, {self(), :test_finished, %{test | time: us}}

exit(:shutdown)

Back to testing!

I write my previous script as a test and import my module so that I can call j_tl on it's own and it works. To test the boundaries, what if I rename my j_tl method to tl, will it be like Ruby and let me redefine anything and everything?


$ mix test

Compiling 1 file (.ex)

** (CompileError) test/cards_test.exs:12: function tl/1 imported from both Cards and Kernel, call is ambiguous

Apparently not. I think I am relieved about that :D

I feel happy that I have a project with proper testing support to play around with. In the next session I'm going to put the "easy" cards aside and pick up the "hard" half of the deck. Time to step things up!


< Previous