Question

In Python's setup.py we can specify dependencies that should be downloaded and installed as eggs by specifying under test_requires the requirements. In Elixir's mix how do I specify dependencies that I want to only use for tests ?

Was it helpful?

Solution

Dependencies in Mix are placed inside a private function called deps, it is not a rule, it is only the default value of the deps entry in the project "section", see below:

def project do
 [ app: :teste,
    version: "0.0.1",
    elixir: "~> 0.12.5-dev",
    deps: deps ] # It's here!
end

You can declare a deps function for test's dependencies and set the deps entry above to deps(Mix.env). This is a way to tell Mix to call deps with a param that come from MIX_ENV enviroment variable.

This snippet could help you to understand better, let me show you:

...

def project do
  [ app: :teste,
    version: "0.0.1",
    elixir: "~> 0.12.5-dev",
    deps: deps(Mix.env) ] # This is important :)
end

...

defp deps(:prod) do
   [ { :a_production_ready_dependency, github: "xpto/cool_project" } ] 
end

defp deps(:test) do
   [ { :some_project, github: "some_project/other" } ]
end

defp deps(_) do
   deps(:prod) ++ [ { :a_devel_dependency, github: "xpto_dev/cool_project_dev" } ]
end

...

Notice that you can still have a default dependency list with defp deps(_), in the case that none of the functions above was pattern-matched. ;)

Then you should call mix as:

MIX_ENV=test mix deps.get

This could not be the best way to do that, but I'm using it often and sounds simple to me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top