0

In my mix.exs I have:

...
def application do
  [
    mod: {MyProject, []},
    extra_applications: [:logger]
  ]
end
...

I use the mod in order to execute my project (e.g.: mix run myproject ARGS). However, when I try to run mix test, it seems to execute MyProject.start with the parameter test.

If I comment out the mod-line inside mix.exs, mix test runs successfully.

Is there a way I can do both, run the project and run the tests without changing the mix.exs? Or do I need to run my tests differently?

tokosh
  • 1,772
  • 3
  • 20
  • 37
  • Does your project define **an application** or not in the first place? – Aleksei Matiushkin Sep 19 '22 at 14:05
  • @AlekseiMatiushkin: I think so. I do have a `defmodule MyProject do use Application` and inside a `def start(_type, _args) do`. Testing some more, I think now, my issue was unrelated. However, I still wonder why `MyProject.start/2` is executed when doing a `mix test`... – tokosh Sep 20 '22 at 03:25
  • 1
    `mix test` starts the application by default. You have to decide whether you want the application started for tests (usually you do, because most things deserving testing are usually not about unit testing,) but if not, you always can stop it with `Application.stop/1`. – Aleksei Matiushkin Sep 20 '22 at 04:54
  • @AlekseiMatiushkin: Thanks for the explanation. Sorry, I am too noob. Where would I need to call `Application.stop/1`? – tokosh Sep 20 '22 at 06:19
  • E. g. in `test_helper.exs`. – Aleksei Matiushkin Sep 20 '22 at 06:26
  • I tried putting that in `test_helper` before or after the `ExUnit.start()`, yet `mix test` still ran the application. I might call it with the wrong parameter (I tried: `MyProject`, `:my_project`). – tokosh Sep 20 '22 at 06:38
  • Ah, indeed, it’d start it after initialization. Then in [`setup_all/1`](https://hexdocs.pm/ex_unit/ExUnit.Callbacks.html#setup_all/1) callback. – Aleksei Matiushkin Sep 20 '22 at 06:49
  • I appreciate the help. But again, I was not able to run the test without execution of the application: I put the `setup_all` into the test-case execution `Application.stop(MyProject)`. – tokosh Sep 20 '22 at 07:38

1 Answers1

0

The issue was unrelated. I did not exit the application properly. Exiting the def start(_type, _args) do function with {:ok, Kernel.self()} makes the mix test run the tests.

tokosh
  • 1,772
  • 3
  • 20
  • 37
  • [`Application.start/2`](https://hexdocs.pm/elixir/Application.html#c:start/2) is a callback that implies you must return the expected value. – Aleksei Matiushkin Sep 20 '22 at 04:56