2

I have using Phoenix + ExUnit for tests.

I have some ExUnit.Case files like DataCase, ConnCase... to define test helpers for my Models, Controllers..., but if I need to define a global helper that it's accessible from all my tests, where should I define it?

rjurado01
  • 5,337
  • 3
  • 36
  • 45

1 Answers1

7

If this is just a matter of a single helper, put it into your test/test_helper.exs.

But as the project grows, it becomes harder to manage. The common approach is to create test/support folder, and modify your mix.exs in the following way:

  1. Create a private function like this:
defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]
  1. Modify your Mix.Project.project/0 callback to include the following key/value pair:
elixirc_paths: elixirc_paths(Mix.env())
  1. Put all the files containing modules needed in test environment only there, as *.ex files.

That way all of them will be compiled in test and discarded in all other environments.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160