Introduction to Elixir Mixes
Roadmap
- Double-check you are ready to get going
- Introduction to Mixes
- Running Tests with Mixes
- Running all the tests
- Running all tests in a file
- Running only 1 test in a file
- Mixes in IEx
- Where to next?
Ready to get going?
This assumes you already have Elixir and Atom installed for your platform. If not, see this post for getting started.
Introducing the Elixir “Mix”
While IEx is super cool, it gets even better once we go a little further. In order to better learn a language and the whole Erlang/Elixir framework, I need a simple project. Conveniently, Elixir has the “mix” command built-in. This is helpful for starting a new Elixir project that follows good practices on project layout and starts you off right with tests built-in. Some of this was taken from the Introduction to Mix.
Running Tests
The default mix new
project has a single passing test as an example.
Run the tests to see the output.
The “dot” on a line by itself shows that 1 test was run and passed. Let’s see what that was.
Examine our tests
In Atom, open the folder for our mix project.
test/test_project_test.exs
What “Failure” looks like:
Create a failing test. Edit, “test/test_project_test.exs” adding test "failure"
.
In a terminal, run “mix text” again:
The “dot” by itself is still there showing one test ran and passed. Now it is followed by output for a failing test. Nice! It shows the code being tested, the “lhs” for “left-hand-side” of the equation and “rhs” for “right-hand-side”. Then stacktrace gives us the line number where it failed.
Running a specific file
In larger projects, you don’t want to run all your tests every time. You can run just a specific file.
Running a specific test
Running a specific test that you are working on is also easy. You do this by specifying the file to run and adding the line number. For this example I added a couple more failing tests to the file to show that 2 were skipped. But the output isn’t cluttered with all the noise of failing tests that I’m not working on yet.
Mix in IEx
Now that we have a mix, IEx can get even better! Working with your project in IEx is helpful for interactively testing and playing with your code.
First lets add some code that we can call.
In our mix project, edit lib/test_project.ex
to add a say_hello
function.
Interact with the Mix in IEx
From within your mix project folder…
We can now start typing “TestProject” and hit “tab” to complete it.
Tab can complete the functions available on a module…
Recompile within IEx for Code Change
Made a code change and you want to test it out in IEX? To pickup the change
you tell the terminal to recompile
. Note: this requires iex -S mix
.
Now we have Mix projects for building projects with best practice organization, a place for tests and an easy way to run them! We’re ready to to start digging into Elixir more deeply.
Next Step…
Now I just want my development environment setup. Next we’ll setup the Atom Editor for Elixir development.