You could experiment with a library that does this: - Provides alternatives for System.get_env/2 and friends to use in their place - Uses https://github.com/dashbitco/nimble_ownership to set and retrieve env in tests, to allow for asynchronous tests
However, system env is still global state. At the end of the day, a better/simpler approach is to probably do something like provide stubs for reading the env value and using Mox or something like that in tests. Andrea > On 27 Mar 2024, at 22:26, José Valim <jose.va...@dashbit.co> wrote: > > I don't see a strong need to make this part of ExUnit, especially because: > > 1. We should avoid mutating the global state in tests > 2. We should avoid reading system environment variables in code (use > config/runtime.exs instead) > > Those can add to their own suites if necessary. :) > > On Wed, Mar 27, 2024 at 4:14 PM Daniel Kukula <daniel.k...@gmail.com > <mailto:daniel.k...@gmail.com>> wrote: >> Currently, tests that use env variables can't be run async because the >> environment is shared. My proposal is to introduce a with_env function that >> will accept a function to execute and a mapping of params that the functions >> in System will accept as env variables: >> Now: >> System.put_env("PORT", "4000") >> assert some logic >> System.delete_env("PORT") >> >> after >> >> with_env(%{"PORT" => "4000}, fn -> >> assert some logic >> end) >> >> -- >> You received this message because you are subscribed to the Google Groups >> "elixir-lang-core" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to elixir-lang-core+unsubscr...@googlegroups.com >> <mailto:elixir-lang-core+unsubscr...@googlegroups.com>. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/elixir-lang-core/3e7dcf24-ae69-47b0-a7b5-281055c70726n%40googlegroups.com >> >> <https://groups.google.com/d/msgid/elixir-lang-core/3e7dcf24-ae69-47b0-a7b5-281055c70726n%40googlegroups.com?utm_medium=email&utm_source=footer>. > > > -- > You received this message because you are subscribed to the Google Groups > "elixir-lang-core" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to elixir-lang-core+unsubscr...@googlegroups.com > <mailto:elixir-lang-core+unsubscr...@googlegroups.com>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4KJsNjCYX0QQvp%2BUKtAVdeUO76g%2BNAq55pyYxsxwrgfQQ%40mail.gmail.com > > <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4KJsNjCYX0QQvp%2BUKtAVdeUO76g%2BNAq55pyYxsxwrgfQQ%40mail.gmail.com?utm_medium=email&utm_source=footer>. -- You received this message because you are subscribed to the Google Groups "elixir-lang-core" group. To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/74F9C3B7-544F-4AA9-954A-B0AA2CDF0852%40gmail.com.