Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Elixir, placing test files alongside their related modules

Tags:

elixir

ex-unit

A typical Elixir project has a structure like this:

project
|-- lib
|   `-- my_module.ex
`-- test
    |-- my_module_test.exs
    `-- test_helper.exs

When writing node projects I like to have my test modules next to the modules they are testing. This is easily configarable using tools like Jest:

project
|-- lib
|   |-- my_module.ex
|   `-- my_module.test.exs
`-- test
    `-- test_helper.exs

Is it possible to have a similar setup in Elixir/Exunit?

like image 250
harryg Avatar asked Jul 26 '18 14:07

harryg


1 Answers

OK, I’ll put it as an answer for the sake of future visitors.

Mix documentation explicitly states

Invoking mix test from the command line will run the tests in each file matching the pattern *_test.exs found in the test directory of your project.

It looks like ExUnit authors strongly discourage developers to put tests somewhere else. As @harryg discovered, it’s still possible, though. project settings have tests_path variable that is checked for where tests are to be found (and test_pattern for the pattern,) defaulted to "test" and "*_test.exs" respectively.

To reset it to some other value, simply update the project callback adding these two lines:

  def project do
    [
      app: @app,
      version: @version,
      elixir: "~> 1.6",
      start_permanent: Mix.env() == :prod,
      ...
      test_paths: ".",
      test_pattern: "*_test.exs" # or something else
    ]
  end
like image 89
Aleksei Matiushkin Avatar answered Sep 19 '22 07:09

Aleksei Matiushkin