Stopping supervisors for tests

Is there a way to prevent specific supervisors from starting when testing?

You can have them start conditionally by checking Mix.env to see if it’s currently :test.

That’d be something you set up in your application module if these are top-level supervisors.

If these are related supervisors and they are usually started together it can be as simple as just:

supervisors = [] ++ if Mix.env != :test, do: [NormalSupervisorStuffHere, OtherSupervisorStuff]
1 Like

Remember that Mix.env is not available outside of compile-time, so that would fail in a release, you probably want:

supervisors = if unquote(Mix.env()) != :test, do: [NormalSupervisorStuffHere, OtherSupervisorStuff], else: []

At the very least (or move it all to a macro or unquote the whole thing and escape it, etc…)

1 Like

It would be more idiomatic to turn that into an Application.get_env(:my_app, :start_child_foo) call and then set that confi value to true or false within the environment config files I think. Plus it lets you avoid macros.