Loading related dependencies marked as test only when MIX_ENV was not initially set to "test"
I want to programmatically run a task mix test
in my Elixir application. This can be done with Mix.Tasks.Test.run/1
, although trying to do this without first setting an environment variable will MIX_ENV
result in the task being denied.
We can install env with System.put_env/2
, but then the application will crash after it finds a link to the module defined in the dependency checked as soon as the test.
How can I load these dependencies in this situation?
source to share
We can set env using System.put_env / 2, but then the application will crash as soon as it finds a link to the module defined in the dependency, marked only as a test.
This is on purpose. You need to set the environment variable before running Mix, otherwise Mix will load the wrong dependencies.
If you are creating a new task, you can tell Mix what is the preferred environment to run it by setting it [preferred_cli_env: [my_task: :test]]
to your project function. Other than that, you have no option other than setting MIX_ENV explicitly.
source to share