Nigel Ramsay

Software Engineer from Wellington, New Zealand

The Elixir equivalent of Rspec’s focus: true

09 December 2017

Rspec’s focus: true can be used to just run a subset of specs. To achieve something similar with Elixir, there are two main approaches that I have used.

Line number targeting

You can specify the test’s line number from the command line. Here’s an example:

$ mix test test/address_builder/au/import/gnaf_importer_test.exs:41

This will run just the test on line 41 of the file.

Targeting with tags

You can use tags to mark a test (or multiple tests) to be run. Here’s an example:

@tag :focus  
test "with mixed case" do  
  assert StringUtil.titleize("weLLington") == "Wellington"  

You can then run your test with the --only option:

$ mix test --only focus

It’s also compatible with the mix_test_watch package, so any edits will be retested straight away.

$ mix --only focus

Further reading