phtrivier

phtrivier

Using Application.get_env / Application.put_env in ExUnit tests

Should it be safe to use Application put/get env in multiple tests ?
Assuming I “clean” the env at the setup of each tests, is it possible that env “leaks” between tests ?
For example this seems to work, but is there any guarantee that it won’t fail once in a blue moon ?

defmodule SUT do
   def act() do
      x = Application.get_env(:app, :params)[:x]
      # .... Use x for whatever 
   end
end

defmodule Case1 do 
  use ExUnit.Case

  setup do 
    Application.put_env(:app, :params, x: nil)
  end

  test "Code that needs x to be 0" do
    Application.put_env(:app, :params, x: 0)
    SUT.act()
  end

  test "Code that needs x to be 1" do
    Application.put_env(:app, :params, x: 1)
    SUT.act()
  end
end

defmodule Case2 do 
  use ExUnit.Case

  setup do 
    Application.put_env(:app, :params, x: nil)
  end

  test "Code that needs x to be 3" do
    Application.put_env(:app, :params, x: 3)
    SUT.act()
  end
end

(In particular, I’m not clear about whetever a Process is run for each test, for each case, etc… and how it might interact with Application config…)

Most Liked

jtompl

jtompl

If you want a code example for the above, it would be like this:

defmodule MyTest do
  use ExUnit.Case, async: false

  setup do
    put_application_env_for_test(:some_app, :some_key, :some_value)
  end

  defp put_application_env_for_test(app, key, value) do
    previous_value = Application.get_env(app, key)
    Application.put_env(app, key, value)
    on_exit(fn -> Application.put_env(app, key, previous_value) end)
  end
end
12
Post #3
hubertlepicki

hubertlepicki

This will break as soon as you use async: true in your tests. Which may or may not be a problem for you.

You probably also want to do the cleanup after the tests, not before. If the tests run in random order, they will jump to some other test file after these tests are done, and the variables may be affecting the behavior in other tests. If you do the cleanups in setup blocks executed before each test here, you want to do it in other test files as well. Otherwise weird things will happen.

Alternative to this is to clean up in on_exit callback: ExUnit.Callbacks — ExUnit v1.20.2

So you can go with what you have provided:

  1. the tests are async: false
  2. the cleanup is done in on_exit block
axelson

axelson

Scenic Core Team

If you use ProcessTree then you can effectively modify the application environment for most processes while keeping the ability to use async: true tests. It works by looking for env first in the process dictionary of the current process, and then the dictionary of all ancestor and caller processes:

https://github.com/jbsf2/process-tree

I’ve been using it quite a bit and it’s a really nice way to allow using the application environment while still using async: true.

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement