Search code examples
elixirelixir-mix

How do I avoid ExUnit.start() error when running mix test


When I run mix test on my project, I get the following error and I don't understand why:

$ mix test
Compiling 2 files (.ex)

== Compilation error in file lib/myproject_web/controllers/email_controller_test.ex ==
** (RuntimeError) cannot use ExUnit.Case without starting the ExUnit application, please call ExUnit.start() or explicitly start the :ex_unit app
    expanding macro: ExUnit.Case.__using__/1
    lib/myproject_web/controllers/email_controller_test.ex:2: MyProjectWeb.EmailControllerTest (module)
    (elixir 1.10.1) expanding macro: Kernel.use/2
    lib/myproject_web/controllers/email_controller_test.ex:2: MyProjectWeb.EmailControllerTest (module)

I already have a test_helper.exs file in lib/ which calls ExUnit.start(). My set up is unusual because I want the tests next to the modules rather than in a separate test folder.


Solution

  • It turns out this error comes from the test file having a .ex extension rather than .exs. With .ex then mix tries to compile it with everything else before running the tests and then complains because ExUnit.start() hasn't been called at compile time.

    With the .exs extension then the file isn't compiled but is run by mix test after the ExUnit.start() has been called from test_helpers.exs.