Run Elixir Tests in Watch Mode
Here's a way to run Elixir tests in watch mode. "Watch mode" is where your tests will re-run automatically when src changes.
Start New Mix Project
The simplest way to get an Elixir project set up for testing is with mix, which comes with Elixir distributions.
mix new my_project
3rd-Party Library Support
The ExUnit
library is built into Elixir for testing. But it doesn't have a built-in way to run tests in watch mode.
But there is a 3rd-party option, mix-test.watch.
When we created a new project, a mix.exs
file was generated, with our dependencies list. To integrate with the library, we'll adjust it to read:
defp deps do
[
{:mix_test_watch, "~> 1.0", only: [:dev, :test], runtime: false}
]
end
Run the Tests in Watch Mode
Now to run the tests in watch mode, instead of mix test
, we run:
mix test.watch
And as we change our code, our tests will re-run against it.
Supposed Built-in Support
Now, do we really need 3rd-party libraries? When I mix help test
, I get the suggestion that I should get watch mode with:
fswatch lib test | mix test --listen-on-stdin
But, at least in my case, the constantly restarts the test process (ie, not just on file save). Do you have any luck with this method?