A couple failed attempts and the documentation here leads me to believe that I have to either define helper modules in test/test_helper.exs
or in one of the other test/*.exs
files nested under a module (that use ExUnit.Case
) in my mix project. Is there a way to define these modules in their own files so that tests can use them, without cluttering up test/test_helper.exs
or putting them under lib/
?
Asked
Active
Viewed 418 times
2

matthiasdenu
- 323
- 4
- 18
1 Answers
4
In your mix.exs
file you define different paths for different environments in the project declaration (inside Mix.Project.project/0
callback, key elixirc_paths
:
defmodule MyApp.MixProject do
use Mix.Project
def project do
[
...
elixirc_paths: elixirc_paths(Mix.env()),
...
]
end
Then you provide different clauses for different environments:
defp elixirc_paths(:test), do: ["lib", "test/helpers"]
defp elixirc_paths(_), do: ["lib"]
The paths above will be added to what Elixir compiles and all the code in test/helpers
dir will become available in the runtime when running the project in test
environment only.

Aleksei Matiushkin
- 119,336
- 10
- 100
- 160
-
Awesome. That worked, thank you. One more detail is that the helper modules should have the `.ex` extension. I figured it out eventually. – matthiasdenu Dec 26 '18 at 10:04
-
@matthiasdenu well, I guessed the mandatory _compiled module_ extension `ex` (as opposed to _elixir script_ `exs`) is out of scope here since it’s the Elixir-wide convention that has basically nothing to do with the OP :) – Aleksei Matiushkin Dec 26 '18 at 10:10